我有一个模型,里面有一些属性/类。现在我想验证一些东西:ASP.NET MVC验证。如何使用嵌套类
<% = Html.TextBoxFor(p=>p.Ideas.Title, new { @class = "width_percent_80" }) %>
<% = Html.ValidationMessageFor(model => model.Ideas.Title) %>
但它将文本框命名为Ideas.Title(而不是标题)。为什么?
我有一个模型,里面有一些属性/类。现在我想验证一些东西:ASP.NET MVC验证。如何使用嵌套类
<% = Html.TextBoxFor(p=>p.Ideas.Title, new { @class = "width_percent_80" }) %>
<% = Html.ValidationMessageFor(model => model.Ideas.Title) %>
但它将文本框命名为Ideas.Title(而不是标题)。为什么?
但它将文本框命名为Ideas.Title(而不是标题)。为什么?
使默认模型绑定能够成功地结合到:
[HttpPost]
public ActionResult Foo(MyViewModel model)
{
if (!ModelState.IsValid)
{
// the model was not valid => redisplay the view
// so that the user can fix his errors
return View(model);
}
// at this stage validation passed => we can access individual properties
// of the view model such as model.Ideas.Title here
...
}
嵌套类会给你一个嵌套的字段名。但是你可以在你的实体.cs文件中添加注释来覆盖这个。
例如: 在Ideas.cs文件,
[Column(name: "title")] //<- add this
public string Title{ get; set; }
当然,你需要使用DataAnnotation命名空间。
using System.ComponentModel.DataAnnotations;
它不起作用 –
我需要生成名称=“标题”,而不是“Ideas.Title”在任何情况下... –
@ user285336,两种可能性:修改您的视图模型,并在根部直接包括Title属性,或写一个自定义的文本框助手,将这样做。 Html.TextBoxFor就是你所看到的。 –