繁体   English   中英

从asp.net mvc中发布的数据中删除前缀

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM