简体   繁体   English

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

[英]Remove prefix from posted data in asp.net mvc

I have a view which looks like below. 我有一个看起来像下面的视图。 Each field has a prefix attached in the name property, but the model in my backend has property without the prefix. 每个字段在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>
}

My Action Method looks something like below : 我的操作方法如下所示:

public ActionResult Save([ModelBinder(typeof(CustomModelBinder))]Employee employee)
        {
            throw new NotImplementedException();
        }

My Model looks like : 我的模型如下:

public class Employee
    {
        public string Name { get; set; }
    }

Can someone help me how to achieve this through custom model binder, i want to strip prefix from each of the posted form items name. 有人可以帮我如何通过自定义模型活页夹实现此目的吗,我想从每个发布的表单项名称中删除前缀。

Posted form data : 表格数据发布:

prefix:prefix
prefix.Name:Hello World!!

I tried below code as well but it's not working. 我也尝试了下面的代码,但是它不起作用。 Can someone explain whats wrong here. 有人可以在这里解释怎么了。

public ActionResult Save([Bind(Prefix = "prefix")]Employee employee)
        {
            throw new NotImplementedException();
        }

Try adding a binding prefix value, like this: 尝试添加绑定前缀值,如下所示:

public ActionResult SetPassword([Bind(Prefix = "Form")] UserSetPasswordForm form)
{
    if (!ModelState.IsValid)
    {
        ....
    }

    ...
}

You could use BindAttribute for his 您可以将BindAttribute用于他的

public ActionResult Submit([Bind(Prefix = "L")] string[] longPropertyName) {

}

there is a similar question to yours here: Asp.Net MVC 2 - Bind a model's property to a different named value 这里有一个与您类似的问题: Asp.Net MVC 2-将模型的属性绑定到其他命名值

There was some strange behavior, when i changed my prefix value it started working. 有一种奇怪的行为,当我更改前缀值时它开始工作。

public ActionResult Save([Bind(Prefix = "**someothervalue**")]Employee employee)
        {
            throw new NotImplementedException();
        }

Thanks everyone for your help. 感谢大家的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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