私はビューを持っており、そのビューの内部には部分ビューを含むdivがあります。MVC5でビューモデルを部分ビューで更新するにはどうすればよいですか?
私の問題はこれです。ユーザーがドロップダウンリストから項目を選択すると、モデルで部分図を読み込みます。ユーザーの変更によって、テキストボックスの一部が変更され、ボタンがクリックされて部分ビュー(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; }
}
EDIT: コントローラコード:
// 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");
}
コントローラコードを投稿できますか? –