我有了几个editorfor/textboxfors为什么不TryUpdateModel()验证textboxfor或更新模型?
视图<% using(Html.BeginForm("New", "Controller", FormMethod.Post)) {%>
<%: Html.ValidationSummary(false) %>
<div>Address</div>
<div><%: Html.EditorFor(model => model.Address)</div>
<div>Zip Code</div>
<div><%: Html.EditorFor(model => model.Zip)</div>
<div>Total Mats</div>
<div><%: Html.EditorFor(model => model.TotalMats)</div>
<input type="image" src="<%: Url.Content("~/Content/Images/submit.png") %>" name="Submit" />
<% } %>
操作:
型号:
[Bind(Include="Name,Date")]
public class Event
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime Date { get; set; }
public int Participants { get; set; }
public int TotalMats { get; set; }
}
所以前两个EditorFors是字符串,而最后是int(在模型上)。这是我注意到的一件事。我不确定它是否相关。但每次我提交时,表单集合都会显示TotalMats字段有值。但是一旦它通过TryUpdateModel,它就不会更新模型的属性。有什么我失踪?在我的一些页面上,它使用了一个int类型的属性。我不得不做一个手动的FormCollection [“key”]检索,但我不得不做额外的验证,只是为了确保它不会弹出非整数值。
你能向我们展示完整视图和模型类吗? – 2012-08-01 05:43:17
你去... – gdubs 2012-08-01 13:12:11