asp.net-mvc-3 – 具有多个强类型部分视图的MVC 3 Razor表格帖子没有绑定
|
我很好奇在一个回复到包含部分View的表单中使用多个强类型部分的方法是否是正确的MVC处理方法.为简洁起见,主视图与以下模型绑定,其中包含其他几个属性和数据注释: 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视图的标记并不完全在下面,但这是部分在下面使用的方式: @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的部分视图在下方,删除了多余的标记. @model Models.Account.UserLogin
<div>
@Html.TextBoxFor(mod => mod.LoginId)
@Html.PasswordFor(mod => mod.Password)
</div>
问题出在注册表单上,AccountSetup属性为null,包含在partials中.但是,如果我将单个模型添加到方法签名中,则会填充它们.我意识到这是因为当字段呈现时ID被更改,因此它们看起来像注册视图的_LoginId,因此它不会映射回AccountSetup模型. 不为accountSetup.UserLogin或accountSetup.SecurityQuestions获取值 [HttpPost]
public ActionResult Register(AccountSetup accountSetup)
{
获取userLogin和securityQuestions的值 [HttpPost]
public ActionResult Register(AccountSetup accountSetup,UserLogin userLogin,SecurityQuestions securityQuestions)
{
问题是如何将这些映射回到包含的Views(AccountSetup)模型的属性,而不必将部分模型添加到方法签名只是为了获取值?在主视图中使用强类型部分视图这是一种不好的方法吗? 解决方法这是因为您的部分视图是强类型的.删除Partials中的@model声明,然后像这样访问Model属性@Html.Partial("_LoginAccount")
然后在你的部分 <div>
@Html.TextBoxFor(mod => mod.UserLogin.LoginId)
@Html.PasswordFor(mod => mod.UserLogin.Password)
</div> (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- 使用Asp.net Web API时,使用DataContract和DataMember属性有
- asp.net-core – 如何使用带有IdentityServer4的ASP.Net标识
- asp.net – 来自TextBox的ActionLink routeValue
- asp.net – 如何防止Azure网站进入睡眠状态?
- asp.net – 实现安全的“记住我”的最佳实践
- asp.net-mvc – 如何从剃刀视图访问My.Resources
- asp.net – 如何防止重播攻击?
- asp-classic – 检索ADO Recordset字段名称(经典ASP)
- asp.net-mvc – 在App_Code中的共享@helper中使用@Html
- asp.net – 如何在没有实体框架的MVC中使用SimpleMembershi
- asp.net – Dropzone没有绑定到模型
- asp.net – ASP .net当前物理位置
- asp.net-mvc – .NET 4.5 MVC RouteCollection.L
- asp.net-mvc – 在html.actionlink上单击转到上一
- asp.net – 完全替换Swashbuckle UI
- asp.net – 在调用“WebSecurity”类的任何其他方
- asp.net-mvc – 是否可以在MVC控制器中的一个操作
- asp.net – 允许 – (破折号)在正则表达式中
- asp.net – ELMAH – 过滤404错误
- asp.net-mvc – 如何使用MVCSiteMap进行隐式本地
