MVC模型绑定的命名约定的子对象? [英] MVC model binding naming convention for child objects?

查看:143
本文介绍了MVC模型绑定的命名约定的子对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用默认的模型绑定的命名约定时,有一个子属性的麻烦。例如:

I'm having trouble with default model binding naming convention when there is a child property. For example:

我有一个视图模型看起来是这样的:

I have a ViewModel which looks something like this:

public class UserViewModel
{
    public User BusinessObject { get; set; }
}

我的用户类有一个名为NetworkLogin的属性。

My User class has a property called "NetworkLogin"

我查看了这样的事情:
                
                    <%:Html.LabelFor(型号=> model.BusinessObject.NetworkLogin)%>
                
                
                    <%:Html.TextBoxFor(型号=> model.BusinessObject.NetworkLogin)%>
                    自动填充
                
                

My View has something like this: <%: Html.LabelFor(model => model.BusinessObject.NetworkLogin)%> <%: Html.TextBoxFor(model => model.BusinessObject.NetworkLogin)%> Auto-Fill

和我的控制器,我喜欢做的,就是

And my controller, what I'd like to do, is

    [HttpGet]
    public ActionResult UserIndex(string networkLogin) { }

问题:
输入参数networkLogin总是空。这是有道理的,因为HTML元素的实际参数名称=BusinessObject.NetworkLogin和id =BusinessObject_NetworkLogin。但是,我不知道是什么参数名,我应该在我的操作方法使用。我已经试过businessObject_NetworkLogin,它并不能工作。

The problem: The input parameter "networkLogin" is always null. This makes sense, because the actual parameter on the html element is name="BusinessObject.NetworkLogin" and id="BusinessObject_NetworkLogin". However, I don't know what parameter name I should use in my action method. I've tried "businessObject_NetworkLogin" and it doesn't work either.

不过,我有这样的解决办法,做工作,但我不喜欢它。我将它添加到我的视图模型:

However, I have this workaround that does work, but I don't like it. I add this to my ViewModel:

    public string NetworkLogin
    {
        get
        {
            if (BusinessObject == null)
                BusinessObject = new User(); 
            return BusinessObject.NetworkLogin;
        } 
        set
        {
            if (BusinessObject == null)
                BusinessObject = new User();
            BusinessObject.NetworkLogin = value;
        }
    }

和我查看网页现在说这个代替。
                    &LT;%:Html.TextBoxFor(型号=> model.NetworkLogin)%>

And my View page now says this instead. <%: Html.TextBoxFor(model => model.NetworkLogin)%>

谁能告诉我正确的命名约定是默认模型绑定,这样我就不必采用上述解决办法?

Can someone tell me what the proper naming convention is for default model binding so that I don't have to employ the above workaround?

感谢您!

推荐答案

指明preFIX使模型粘结剂知道, BusinessObject.NetworkLogin 查询字符串参数实际上指的是 networkLogin 这是你作为行动参数使用什么

Indicate the prefix so that the model binder knows that the BusinessObject.NetworkLogin query string parameter actually refers to networkLogin which is what you use as action argument

public ActionResult UserIndex(
    [Bind(Prefix = "BusinessObject")] string networkLogin
) 
{ 
    ...
}

或重用您的视图模型:

public ActionResult UserIndex(UserViewModel model) 
{ 
    // TODO: use model.BusinessObject.NetworkLogin
    // which is gonna be correctly bound here
    ...
}

至于你的解决方法而言,一旦你把我的两个建议,一进动作视图模型属性真的应该是这样的:

As far as your workaround is concerned, once you put one of my two suggestions into action your view model property should really look like this:

public string NetworkLogin { get; set; }

这篇关于MVC模型绑定的命名约定的子对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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