我有一些看似非常简单的东西不起作用。
我有一个模型
public class Name: Entity
{
[StringLength(10), Required]
public virtual string Title { get; set; }
}
public class Customer: Entity
{
public virtual Name Name { get; set; }
}
视图模型
public class CustomerViweModel
{
public Customer Customer { get; set; }
}
一个看法
<% using(Html.BeginForm()) { %>
<%= Html.LabelFor(m => m.Customer.Name.Title)%>
<%= Html.TextBoxFor(m => m.Customer.Name.Title)%>
<button type="submit">Submit</button>
<% } %>
和一个控制器
[HttpPost]
public ActionResult Index([Bind(Prefix = "Customer")] Customer customer)
{
if(ModelState.IsValid)
Save
else
return View();
}
无论我输入什么作为标题(null,或 > 10 个字符的字符串),ModelState.IsValid 始终为真。Customer 对象中的 Title 字段有一个值,所以数据正在传递,但没有被验证?
有什么线索吗?