页面上的Asp.net MVC剃刀多个窗体 [英] Asp.net MVC Razor more than one form on a page
问题描述
哟
我有我的网站注册页面 - 在页面的顶部是现有用户的登录表单。在主要区域有登记表。
I have a registration page on my site - at the top of the page is a login form for existing users. In the main area there is the registration form.
登录都是以 @model ViewModels.LoginViewModel
的局部视图
注册是也部分与 @model ViewModels.RegViewModel
The login are is a partial view with @model ViewModels.LoginViewModel
The registration are is also a partial with @model ViewModels.RegViewModel
里面有这些谐音的主网页是 @model ViewModels.RegPageViewModel
The main page which houses these partials is a view with @model ViewModels.RegPageViewModel
此视图模型如下:
public class RegViewModel
{
public RegisterVm RegisterVm { get; set; }
public LoginVm LoginVm { get; set; }
}
当我提交页面的注册部分(它的作用是寄存器/捕获 - 接收动作需要一个RegisterVm)到它的控制器,它抱怨被传递了错误的视图模型
When I submit the registration part of the page (it's action is register/capture - the receiving action expects a RegisterVm) to it's controller it complains about being passed the wrong viewmodel
什么是与子视图和视图模型的交易?是否有处理这一标准的做法?
What's the deal with subviews and their viewmodel? Is there a standard approach to dealing with this?
我应该有一个URL提交此网页,其中数字,如果它是一个登录请求或注册请求,然后相应地处理这个职位吗?这似乎凌乱我虽然...
Should I have one submit URL for this page which figures out if it's a login request or a register request and then handles the post accordingly? That seems messy to me though...
http://monobin.com/__d33cf45a4 - RegisterVm.cs(LoginVm.cs为pretty大致相同的,因为这)
http://monobin.com/__d33cf45a4 - RegisterVm.cs (LoginVm.cs is pretty much the same as this)
http://monobin.com/__m69132f76 - RegPageVm.cs
http://monobin.com/__m69132f76 - RegPageVm.cs
Register.cshtml:
Register.cshtml:
@model xxxx.ViewModels.RegPageVm
@{
View.Title = "Register";
Layout = "~/Views/Shared/_BareBones.cshtml";
}
<link rel="stylesheet" href="@Url.Content("~/Public/Css/signup.css")" type="text/css" />
<div id="sign-up-container">
<div id="sign-up-box">
<div id="sign-up-box-left">
<img src="@Url.Content("~/Public/Images/Signup_176x81.png")" />
</div>
<div id="sign-up-box-right">
@Html.Partial("_Register")
</div>
</div>
</div>
<div class="clear">
</div>
_Register.cshtml:
_Register.cshtml:
@model xxxx.ViewModels.RegisterVm
@using (Html.BeginForm("Capture", "Register", FormMethod.Post))
{
<table class="sign-up-box-inner">
<tr>
<td class="label-area">
@Html.LabelFor(x => x.Email)
</td>
<td class="field-area">
@Html.TextBoxFor(x => x.Email, new { @class = "login-input", title = "Enter Name" })
</td>
</tr>
<tr>
<td class="label-area">
@Html.LabelFor(x => x.Password)
</td>
<td class="field-area">
@Html.PasswordFor(x => x.Password, new { @class = "login-input", title = "Enter Name" })
</td>
</tr>
<tr>
<td class="label-area">
@Html.LabelFor(x => x.UserName)
</td>
<td class="field-area">
@Html.TextBoxFor(x => x.UserName, new { @class = "login-input", title = "Enter Name" })
</td>
</tr>
<tr>
<td colspan="2">
<input type="image" src="../../Public/Images/Submit_150x47.png" class="submit-button" />
</td>
</tr>
</table>
@Html.AntiForgeryToken()
}
最后RegisterController.cs:
And finally RegisterController.cs:
public class RegisterController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost, ValidateAntiForgeryToken]
public ActionResult Capture(RegisterVm registerVm)
{
if (!ModelState.IsValid)
{
return View("index", new RegPageVm()
{
LoginVm = new LoginVm(),
RegisterVm = registerVm
});
}
return RedirectToAction("index", "Event");
}
}
W://
推荐答案
您需要确保表单元素(如文本框等)应具有相同的ID作为RegisterVM和LoginVM属性。你的理论是正确的,但我想你可能会使得MVC的命名约定是错误的。
You need to ensure that the form elements (like the textbox etc) should have the same id as the RegisterVM and LoginVM properties. Your theory is right but I think you might be making a mistake in the naming convention of MVC.
如果你可以分享你的观点code +虚拟机类,那么我们将能够更好地帮助
If you can share your view code + the VM classes, then we'll be able to help better.
编辑:
在你code展望,我认为你应该在视图模型传递给您的局部视图。例如像下面的行认为应该是这个样子>
Looking at your code I think you should be passing the view model to your partial view. Like for example the following line believe should be like this >
@ Html.Partial(_注册,Model.RegisterVm)
这篇关于页面上的Asp.net MVC剃刀多个窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!