谢谢大家的努力。我不得不创建新的模型,并包裹在里面的两个模型注册和登录。
它看起来像how to work with two forms in a single view帮了我很多。
但是,我发布一个新手的完整解决方案。
CustomerController:
加载页面
public ActionResult Login(bool? checkoutAsGuest)
{
var loginModel= new LoginModel();
loginModel.UsernamesEnabled = _customerSettings.UsernamesEnabled;
loginModel.CheckoutAsGuest = checkoutAsGuest.GetValueOrDefault();
loginModel.DisplayCaptcha = _captchaSettings.Enabled && _captchaSettings.ShowOnLoginPage;
var registerModel = new RegisterModel();
PrepareCustomerRegisterModel(registerModel, false);
registerModel.Newsletter = _customerSettings.NewsletterTickedByDefault;
return View(new LoginRegisterModel { LoginModel = , RegisterModel = registerModel });
}
登录POST:
public ActionResult Login(LoginModel model, string returnUrl, bool captchaValid)
{
// Previous code as it is
// Important! I Added this new line - to handle validation problems
ModelState.Add("LoginValidation", null);
//If we got this far, something failed, redisplay form
model.UsernamesEnabled = _customerSettings.UsernamesEnabled;
model.DisplayCaptcha = _captchaSettings.Enabled && _captchaSettings.ShowOnLoginPage;
var registerModel = new RegisterModel();
PrepareCustomerRegisterModel(registerModel, false);
//enable newsletter by default
registerModel.Newsletter = _customerSettings.NewsletterTickedByDefault;
return View(new LoginRegisterModel { LoginModel = model, RegisterModel = registerModel });
}
雷吉斯之三:
public ActionResult Register()
{
//check whether registration is allowed
if (_customerSettings.UserRegistrationType == UserRegistrationType.Disabled)
return RedirectToRoute("RegisterResult", new { resultId = (int)UserRegistrationType.Disabled });
var model = new RegisterModel();
PrepareCustomerRegisterModel(model, false);
model.Newsletter = _customerSettings.NewsletterTickedByDefault;
var loginModel = new LoginModel();
loginModel.UsernamesEnabled = _customerSettings.UsernamesEnabled;
loginModel.DisplayCaptcha = _captchaSettings.Enabled && _captchaSettings.ShowOnLoginPage;
return View("Login", new LoginRegisterModel { RegisterModel = model, LoginModel = loginModel });
}
注册POST:
public ActionResult Register(RegisterModel model, string returnUrl, bool captchaValid, FormCollection form)
{
// previous code as it is
// added this line to handle validations
ModelState.Add("RegisterValidation", null);
//If we got this far, something failed, redisplay form
PrepareCustomerRegisterModel(model, true, customerAttributesXml);
var loginModel = new LoginModel();
loginModel.UsernamesEnabled = _customerSettings.UsernamesEnabled;
//loginModel.CheckoutAsGuest = checkoutAsGuest.GetValueOrDefault();
loginModel.DisplayCaptcha = _captchaSettings.Enabled && _captchaSettings.ShowOnLoginPage;
return View("Login", new LoginRegisterModel { RegisterModel = model, LoginModel = loginModel });
}
然后创建了两个局部视图 - _LoginModel.cshtml
和_registerModel.cshtml
。在意见我只加一个额外的行
_LoginModel.cshtml:
if (!MvcHtmlString.IsNullOrEmpty(validationSummary) && ViewData.ModelState.ContainsKey("LoginValidation"))
{
<div class="message-error">@validationSummary</div>
}
_RegisterModel.cshtml
@if (!MvcHtmlString.IsNullOrEmpty(validationSummary) && ViewData.ModelState.ContainsKey("RegisterValidation"))
{
<div class="message-error">@validationSummary</div>
}
,最后登录页面
登录。 cshtml
更换注册按钮(左面板)
@using (Html.BeginForm("Register", "Customer", FormMethod.Post)){
<!-- divs and other elements -->
@Html.Partial("_RegisterModel", Model.RegisterModel)
<!-- divs and other elements -->
}
和登录形式
@using (Html.BeginForm("Login", "Customer", new { returnUrl = Request.QueryString["returnUrl"] }, FormMethod.Post))
{
<!-- divs and other elements -->
@Html.Partial("_LoginModel", Model.LoginModel)
<!-- divs and other elements -->
}
那是因为你在你的注册行动返回查看(模型)。您可以在注册操作中返回部分视图。或者更好的做法是将你的注册作为局部视图。 – jomsk1e
@ jomsk1e我试图返回部分视图。但没有变化。 –
返回部分视图并更改您的表单以在您的登录操作上发布,而不是在此行上注册:'使用(Html.BeginForm(“Register”,“Customer”,FormMethod.Post))' – jomsk1e