[英]Remove prefix from posted data in asp.net mvc
我有一個看起來像下面的視圖。 每個字段在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();
}
嘗試添加綁定前綴值,如下所示:
public ActionResult SetPassword([Bind(Prefix = "Form")] UserSetPasswordForm form)
{
if (!ModelState.IsValid)
{
....
}
...
}
您可以將BindAttribute
用於他的
public ActionResult Submit([Bind(Prefix = "L")] string[] longPropertyName) {
}
這里有一個與您類似的問題: Asp.Net MVC 2-將模型的屬性綁定到其他命名值
有一種奇怪的行為,當我更改前綴值時它開始工作。
public ActionResult Save([Bind(Prefix = "**someothervalue**")]Employee employee)
{
throw new NotImplementedException();
}
感謝大家的幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.