我必须有一些设置不正确,因为我无法获得的UpdateModel功能基础上通过的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();
}
}
,你可以看到,如果我手动分配每个属性它工作正常,所以我没有设置什么可以让模型更新表单值?
感谢,
马克
谢谢,这是我的情况。 – ca8msm 2012-08-13 07:37:19