Vielen Dank für Ihre Bemühungen.Ich musste ein neues Modell erstellen und die beiden Modelle Register und Login in das verpacken.
es sieht aus wie how to work with two forms in a single view hat mir sehr geholfen.
Allerdings posten ich komplette Lösung für einen Neuling.
CustomerController:
Laden der Seite
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 });
}
Anmeldung 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 });
}
Regis ter:
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 });
}
Register 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 });
}
erstellt dann zwei Teilansichten - _LoginModel.cshtml
und _registerModel.cshtml
. In den Ansichten habe ich nur eine zusätzliche Zeile
_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>
}
und schließlich, Login-Seite
Anmeldung. cshtml
ersetzt Register-Taste (links mit)
@using (Html.BeginForm("Register", "Customer", FormMethod.Post)){
<!-- divs and other elements -->
@Html.Partial("_RegisterModel", Model.RegisterModel)
<!-- divs and other elements -->
}
und Login-Formular mit
@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 -->
}
es ist, weil Sie View (Modell) im Register Aktion zurückkehren. Sie können eine Teilansicht in Ihrer Aktion Registrieren zurückgeben. Oder noch besser machen Sie Ihre Registrierung als Teilansicht. – jomsk1e
@ jomsk1e Ich habe versucht, Teilansicht zurückzugeben. aber keine Veränderung. –
Teilansicht zurückgeben und das Formular so ändern, dass es auf Ihrer Login-Aktion posten statt auf dieser Zeile registrieren: 'using (Html.BeginForm (" Register "," Customer ", FormMethod.Post))' – jomsk1e