如何在MVC5的局部视图中更新视图模型?

时间:2016-04-25 18:54:35

标签: asp.net-mvc asp.net-mvc-5

我有一个视图,在该视图中我有一个包含局部视图的div。

我的问题是这个。用户从下拉列表中选择一个项目,然后使用模型加载局部视图。用户更改会更改某些文本框并单击该按钮以提交部分视图(位于Html.BeginForm中)。

当我在控制器中检查模型时,模型不包含用户所做的更改。

为什么模型不反映用户所做的更改?

在主视图中:

<div id="personInfo" style="display:none;"></div>

我的部分观点:

    @model MyProject.MyModel

    @(Html.Kendo().DropDownList().Name("ddlFilters")
                                .AutoBind(true)
                                .OptionLabel("--- Select Filter ---")
                                .DataValueField("ID")
                                .DataTextField("MYFILTER")
                                .DataSource(ds =>
                                    ds.Read(r => r.Action("GetPersonFilters", "Home"))
                                )
                                .Events(x => x.Select("ddlFilters_onSelect"))
                        )

    @using (Html.BeginForm("PersonAction", "Home", FormMethod.Post, new { @class = "form-horizontal", id = "personForm" }))
            {
                 // Strongly typed Kendo fields. Several DropDownListFor and TextBoxFor
                 @Html.Kendo().TextBoxFor(x => x.FirstName).HtmlAttributes(new { @class = "form-control kendoTextBox required " })
                // Button to post the form data to the controller.
            }

我的Javascript:

function ddlFilters_onSelect(e) {
    var itm = this.dataItem(e.item);

    clearForm();

    if (itm.ID > 0) {
        // Ajax call to get data....
        $.ajax({
            url: "/Home/GetPerson",
            type: "GET",
            data: { "myID": itm.ID }
        })
        .done(function (result) {
            //var aaa = data;      
            $("#personInfo").html(result);
        })
        .fail(function (xhr, status, err) {
            alert(xhr.responseText);
        });
    }
};

型号:

 public partial class MyModel
    {
        public decimal ID { get; set; }
        public string FirstName{ get; set; }
        public string LastName{ get; set; }
        public string MiddleName{ get; set; }
    }

编辑: 控制器代码:

 // Initial call to main view
    public ActionResult CreateNewPerson()
    {
        return View();
    }

    // Call to load Partial View initially
    public PartialViewResult GetPersonInfo()
    {
        return PartialView("_PersonForm", new MyModel());
    }

    // Call to load partial view with data
    public PartialViewResult GetPerson(int myID)
    {
        myData = GetFromDB(myID);
        return PartialView("_PersonForm", myData);
    }

    // Method to save partial form
    [HttpPost]
    public ActionResult PersonAction(MyModel filter)
    {           

        if (ModelState.IsValid)
        {
            // Go update DB
        }

        return View("CreateNewPerson");
    }

1 个答案:

答案 0 :(得分:1)

这不完全是您描述的情景,但这是我的团队使用部分内容的方式:

1)在主视图的ViewModel中,为部分视图的模型添加属性(例如MyModel)。

2)在cshtml中调用部分View时,请确保告诉MVC绑定部分视图内容的位置:

  @Html.Partial("_PersonAction", Model.MyModel, new ViewDataDictionary(Html.ViewData) {
      TemplateInfo = new TemplateInfo { HtmlFieldPrefix = Html.NameFor(m => m.MyModel).ToString() }
  })

注意我们如何使用TemplateInfo为partial设置正确的上下文,因此在partial中呈现的输入以正确的名称作为前缀以使modelbinding工作。例如。 <input name="MyModel.FirstName"> 你可以在javascript中假装这个,但不要问我怎么做。

3)我们的控制器动作接受主页面的ViewModel。 <form>位于主页面上并围绕部分调用。