FormCollectionを介して渡された情報に基づいてモデルを正しく更新するためのUpdateModel関数を取得できないため、何かが正しく設定されていない必要があります。
私の見解は次のようになります:
@model NSLM.Models.Person
@{
ViewBag.Title = "MVC Example";
}
<h2>My MVC Model</h2>
<fieldset>
<legend>Person</legend>
@using(@Html.BeginForm())
{
<p>ID: @Html.TextBox("ID", Model.ID)</p>
<p>Forename: @Html.TextBox("Forename", Model.Forename)</p>
<p>Surname: @Html.TextBox("Surname", Model.Surname)</p>
<input type="submit" value="Submit" />
}
</fieldset>
私のモデルは:
namespace NSLM.Models
{
public class Person
{
public int ID;
public string Forename;
public string Surname;
}
}
そして私のコントローラーは:
[HttpPost]
public ActionResult Details(FormCollection collection)
{
try
{
// TODO: Add update logic here
Models.Person m = new Models.Person();
// This doesn't work i.e. the model is not updated with the form values
TryUpdateModel(m);
// This does work
int.TryParse(Request.Form["ID"], out m.ID);
m.Forename = Request.Form["Forename"];
m.Surname = Request.Form["Surname"];
return View(m);
}
catch
{
return View();
}
}
各プロパティを手動で割り当てると正常に機能することがわかりますが、モデルがフォーム値で更新されるように設定していないものは何ですか?
ありがとう、
マーク