绑定到formValue不同的名称,ASP.NET MVC的财产 [英] Bind formValue to property of different name, ASP.NET MVC

查看:126
本文介绍了绑定到formValue不同的名称,ASP.NET MVC的财产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有绑定有不同的ID的从类属性传递给控制器​​的形式值的方法。

I was wondering if there was a way to bind form values passed into a controller that have different Id's from the class properties.

形式职位与人的控制器为具有属性名称,但实际的表单文本参数具有PersonName的,而不是名称的标识。

The form posts to a controller with Person as a parameter that has a property Name but the actual form textbox has the id of PersonName instead of Name.

我该如何正确绑定呢?

推荐答案

您可能对这种模式自定义的模型绑定。

You could have your own custom model binder for that model.

public class PersonBinder : IModelBinder {
    public object BindModel(ControllerContext controllerContext, 
        ModelBindingContext bindingContext) {
            return new Person { Name =
                  controllerContext.HttpContext.Request.Form["PersonName"] };
    }
}

和你的行动:

public ActionResult myAction([ModelBinder(typeof(PersonBinder))]Person m) {
        return View();
}

这篇关于绑定到formValue不同的名称,ASP.NET MVC的财产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆