MVC 3剃须刀表单提交瓦特/多强类型的局部视图不具约束力 [英] MVC 3 Razor Form Post w/ Multiple Strongly Typed Partial Views Not Binding

查看:146
本文介绍了MVC 3剃须刀表单提交瓦特/多强类型的局部视图不具约束力的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇是否使用形式回发到部分包含视图是正确的MVC的方法来做事内的多个强类型谐音的方法。其主要观点是必然带有不再赘述其他几个属性和数据注解以下型号:

I'm curious about whether the approach of using multiple strongly typed partials within a form that posts back to the partial containing View is the right MVC approach to doing things. The main View is bound with the following model with several other properties and data annotations omitted for brevity:

public class AccountSetup : ViewModelBase
{
    public bool TermsAccepted { get; set; }
    public UserLogin UserLogin { get; set; }
    public SecurityQuestions SecurityQuestions { get; set; }
}

public class UserLogin
{
    public string LoginId { get; set; }
    public string Password { get; set; }
}

主要Register.cshtml观的标记不是完全的下方,但是这是怎样的谐音如下使用:

The markup of the main Register.cshtml View isn't totally below, but this is how the partials are used below:

@model Models.Account.AccountSetup

. . . <pretty markup> . . . 

@using (Html.BeginForm("Register", "Account", FormMethod.Post))
{ 
     . . . <other fields and pretty markup> . . . 

     @Html.Partial("_LoginAccount", Model.UserLogin)
     @Html.Partial("_SecurityQuestions", Model.SecurityQuestions)

     <input id="btnContinue" type="image" />
}

仅供参考_LoginAccount的局部视图下方是过剩的标记去掉。

Just for reference the partial view of _LoginAccount is below with excess markup removed.

@model Models.Account.UserLogin

<div>
     @Html.TextBoxFor(mod => mod.LoginId)

     @Html.PasswordFor(mod => mod.Password)
</div>

问题是对表单提交到注册的AccountSetup属性为null载于该分音。但是,如果我的个别车型添加到方法的签名,他们得到填补。我知道这是因为当领域呈现ID被改变,因此它们看起来像_LoginId为注册查看,因此它不会映射回AccountSetup模式。

The problem is on the form post to the Register the AccountSetup properties are null that were contained in the partials. However, if I add the individual models to the method signature they get filled. I realize it's because when the fields render the ID gets changed so they look like _LoginId for the Register View so it doesn't map back to the AccountSetup model.

没有得到值回accountSetup.UserLogin或accountSetup.SecurityQuestions

Doesn't get values back for accountSetup.UserLogin or accountSetup.SecurityQuestions

    [HttpPost]
    public ActionResult Register(AccountSetup accountSetup)
    {

获取值回USERLOGIN和securityQuestions

Gets values back for userLogin and securityQuestions

    [HttpPost]
    public ActionResult Register(AccountSetup accountSetup, UserLogin userLogin, SecurityQuestions securityQuestions)
    {

是怎么一回包含视图(AccountSetup)模型的属性映射这些无需部分的模型添加到方法签名只是为了让后面的值的问题?这是使用一个主视图中的强类型的局部视图个不错的办法?

The question is how does one map these back to the containing Views (AccountSetup) model's properties without having to add the partial's models to the method signature just to get the values back? Is this a bad approach using strongly typed partial views within a main view?

推荐答案

所有的谐音意见应与同一视图模型(AccountSetup你的情况)是强类型:

All partials views should be strongly typed with the same view model (AccountSetup in your case) :

@model Models.Account.AccountSetup

@Html.TextBoxFor(mod => mod.UserLogin.LoginId)
@Html.PasswordFor(mod => mod.UserLogin.Password)

然后:

@Html.Partial("_LoginAccount", Model)

这篇关于MVC 3剃须刀表单提交瓦特/多强类型的局部视图不具约束力的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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