我有一个如下所示的视图。每个字段都在 name 属性中附加了一个前缀,但我后端中的模型具有没有前缀的属性。
@using (Html.BeginForm("Save", "Home", FormMethod.Post))
{
@Html.ValidationSummary(true)
<fieldset>
<input type="hidden" name="prefix" value="prefix"/>
<input type="text" id="prefix.Name" name="prefix.Name"/>
<input type="submit" value="submit" />
</fieldset>
}
我的操作方法如下所示:
public ActionResult Save([ModelBinder(typeof(CustomModelBinder))]Employee employee)
{
throw new NotImplementedException();
}
我的模型看起来像:
public class Employee
{
public string Name { get; set; }
}
有人可以帮助我如何通过自定义模型绑定器来实现这一点,我想从每个发布的表单项名称中去除前缀。
发布的表格数据:
prefix:prefix
prefix.Name:Hello World!!
我也尝试了下面的代码,但它不起作用。有人可以解释这里出了什么问题。
public ActionResult Save([Bind(Prefix = "prefix")]Employee employee)
{
throw new NotImplementedException();
}