部分视图不执行控制器的操作

时间:2012-10-01 20:16:41

标签: asp.net-mvc asp.net-mvc-4 partial-views

我有以下部分视图,我试图调用视图内的控制器(“消息”)的操作(“搜索”)(“消息/创建”)。

我似乎无法完全控制控制器的动作。我已经尝试过这么多组合,但似乎无法随心所欲。

部分视图意味着有一个文本框和按钮,用于过滤HTML表格。

部分(“_ProfileList.cshtml”)

@model MyApp.Models.MessagingModels.ViewModels.CreateMessageViewModel
@{
    var altLine = false;
}

<div>
    @using (Ajax.BeginForm("Search", "Messages", new AjaxOptions { UpdateTargetId = "dvProducts", HttpMethod = "Post" }))
    {
        <label for="Searchbox">Find Profile :</label>
        @Html.TextBox("SearchString", ViewBag.CurrentFilter as string)
        <input class="ui-state-default ui-corner-all" type="submit" value="Search" />
    }
    <br />
    <br />
</div>
<div id="dvProducts" class="datagrid">
    <table>
        <thead>
            <tr>
                <th>&nbsp;</th>
                <th>Profile Name</th>
                <th>Description</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model.ListOfProfiles)
            {
                <tr class='@(altLine ? "alt" : "norm")'>
                    <td>
                        <input type="radio" name="SelectedProfile" value="@item.ID" id="SelectedProfile_@item.ID" /></td>
                    <td>
                        @if (item.Favourite)
                        {
                            <img src="@Url.Content("~/Images/favourite-star.png")" alt="Favourite" />
                        }

                        @Html.DisplayFor(modelItem => item.Name)</td>
                    <td>@Html.DisplayFor(modelItem => item.Description)</td>
                </tr>
            }
        </tbody>
        <tfoot>
        </tfoot>
    </table>
</div>

CreateMessageViewModel

public class CreateMessageViewModel
{
        private List<ProfileModels.DataModels.Profile> _listOfProfiles; 


        public List<ProfileModels.DataModels.Profile> ListOfProfiles 
        {
            get
            {
                using(var ctx = new EFDataContext())
                {
                    var tmp = ctx.Profiles.OrderBy(p => p.Name).ToList();
                    //_listOfProfiles = ctx.Profiles.OrderBy(p => p.Name).ToList().ToPagedList(1, 10);
                    return _listOfProfiles;
                }
            }
            set { _listOfProfiles = value; }
        }


       //..removed for brevity


}

消息视图

@model MyApp.Models.MessagingModels.ViewModels.CreateMessageViewModel
@{
    ViewBag.Title = "Create";
}
<div class="grid_12">

    @using (Html.BeginForm())
    {
        <div id="SignupForm">
            <fieldset>
                <legend>Profile</legend>
                <strong>What profile would you like to target?</strong>
                 @{ Html.RenderPartial("_ProfilesList", Model); }
            </fieldset>
            <fieldset>
                <legend>Region</legend>
            </fieldset>
            <fieldset>
                <legend>Message</legend>
                <strong>What type of message do you want to send?</strong><br />
                <div id="message-type-selector">
                    <input type="radio" name="messagetype" value="SMS" id="message-type-sms" checked="checked" />
                    <label for="message-type-sms">SMS</label><br />
                    <input type="radio" name="messagetype" value="Email" id="message-type-email" />
                    <label for="message-type-email">Email</label><br />
                </div>
                <br />
                <div id="Message-Options-SMS">
                    @Html.LabelFor(model => model.SMSMessageText)
                    <br />
                    @Html.EditorFor(model => model.SMSMessageText)
                    <div id="smscharactersremaining">100 characters remaining</div>

                    <button id="check-textarea">
                        Check Spelling
                    </button>
                    &nbsp;
                    <span class="loading">loading..</span>



                </div>
                <div id="Message-Options-Email">
                    @Html.LabelFor(model => model.EmailMessageText)
                    <br />
                    <textarea id="EmailMessageText" name="EmailMessageText" cols="50" rows="15"></textarea>
                    <link href="@Url.Content("~/content/jHtmlArea.css")" rel="stylesheet" type="text/css" />
                    <script src="@Url.Content("~/Scripts/jHtmlArea-0.7.5.min.js")" type="text/javascript" ></script>
                    <link href="@Url.Content("~/content/jHtmlArea.css")" rel="stylesheet" type="text/css" />
                    <link href="@Url.Content("~/Content/jHtmlArea.ColorPickerMenu.css")" rel="stylesheet" />
                    <script src="@Url.Content("~/Scripts/jHtmlArea.ColorPickerMenu-0.7.0.min.js")" type="text/javascript" ></script>




                </div>

            </fieldset>
            <fieldset>
                <legend>Tracking</legend>

            </fieldset>
            <fieldset>
                <legend>Confirmation</legend>

            </fieldset>
            <p>@Html.ValidationSummary(false)
                <input id="SaveAccount" type="button" value="Submit form" />
            </p>
        </div>
    }
</div>
<div class="clear"></div>

MessagesController

public class MessagesController : Controller
    {
        private readonly EFDataContext db = new EFDataContext();

        public ActionResult Search(string CurrentFilter)
        {
            var model = from p in db.Profiles
                        where p.Name.Contains(CurrentFilter) || p.Description.Contains(CurrentFilter)
                        select p;


            var vmm = new CreateMessageViewModel
                          {
                              ListOfProfiles = model.ToList()
                          };

            return PartialView("_ProfilesList", vmm);
        }
}

1 个答案:

答案 0 :(得分:3)

你有几个问题,但我认为最大的问题是你有嵌套表格。您的Ajax.BeginForm在另一个Html.BeginForm中呈现。

嵌套表格不是合法的HTML,浏览器可能会也可能不会尊重它们。