我有一个Index页面,它有两个部分视图_Register和_Login。我有三个viewmodel,即LoginViewModel,RegisterViewModel,LoginOrRegisterViewModel。现在,我希望如果modelstate无效,我想重定向到具有登录和注册形式的索引页面。但是,这里,它重定向到注册的视图或在其他视图模型中使用验证登录。请指导我关于正确返回类型和正确的视图。
Index.cshtml
@model Project.ViewModels.LoginOrRegisterModel
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@Html.Partial("_Register", Model.Registermodel)
@Html.Partial("_Login", Model.Loginmodel)
_Register.cshtml
@model ProjectHub.ViewModels.RegisterModel
@using (Html.BeginForm("Register", "account", FormMethod.Post))
{
<div class="label">@Html.Label("Name")</div>
<div class="field">@Html.TextBoxFor(m => m.FullName)</div>
<div class="error">@Html.ValidationMessageFor(model => model.FullName)</div>
//similar html for other attributes
<input class="field" id="submit" type="submit" value="Sign Up" />
@Html.ValidationSummary()
}
_Login.cshtml
@model ProjectHub.ViewModels.LoginModel
@using (Html.BeginForm("Login", "account", FormMethod.Post))
{
@Html.ValidationSummary(true)
<div class="label">@Html.Label("Username")</div>
<div class="field">@Html.TextBoxFor(m => m.Username)</div>
<div class="error">@Html.ValidationMessageFor(model => model.Username)</div>
<div class="label">@Html.Label("Password")</div>
<div class="field">@Html.PasswordFor(m => m.Password)</div>
<div class="error">@Html.ValidationMessageFor(model => model.Password)</div>
<input class="field" id="submit" type="submit" value="Login" />
}
的ViewModels
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace Project.ViewModels
{
public class LoginModel
{
[Required(ErrorMessage="Username is Required")]
public string Username { get; set; }
[Required(ErrorMessage = "Password is Required")]
public string Password { get; set; }
}
public class RegisterModel
{
[Required(ErrorMessage = "Username is Required")]
public string Username { get; set; }
[Required(ErrorMessage = "Password is Required")]
public string Password { get; set; }
[Required(ErrorMessage = "Password is Required")]
public string ConfirmPassword { get; set; }
[Required(ErrorMessage = "Name is Required")]
public string FullName { get; set; }
[Required(ErrorMessage = "Email is Required")]
public string Email { get; set; }
[Required(ErrorMessage = "Country is Required")]
public string Country { get; set; }
}
public class LoginOrRegisterModel
{
public LoginModel Loginmodel { get; set; }
public RegisterModel Registermodel { get; set; }
}
}
的AccountController
public ActionResult Index()
{
LoginOrRegisterModel model = new LoginOrRegisterModel();
model.Loginmodel = new LoginModel();
model.Registermodel = new RegisterModel();
return View(model);
}
[HttpPost]
public ActionResult Index(LoginOrRegisterModel model)
{
return View(model);
}
[HttpGet]
public ViewResult Login()
{
LoginModel model = new LoginModel();
return View("_Login",model);
}
[HttpPost]
public ActionResult Login(LoginModel usermodel)
{
if (ModelState.IsValid)
{
//some validation loic
return RedirectToAction("Index", "Home");
}
return View("Index", new LoginOrRegisterModel() { Loginmodel = usermodel, Registermodel = new RegisterModel() });
}
else
{
ModelState.AddModelError("", "Invalid Username/Password");
return View("Index",new LoginOrRegisterModel() { Loginmodel = usermodel, Registermodel = new RegisterModel() });
}
}
public ViewResult Register()
{
RegisterModel model = new RegisterModel();
return View("_Register",model);
}
[HttpPost]
public ActionResult Register(RegisterModel usermodel)
{
if (!ModelState.IsValid)
{
return View("Index", new LoginOrRegisterModel() { Loginmodel = new LoginModel(), Registermodel = usermodel });
}
try
{
//Some logic for validation
return RedirectToAction("activationemail", new { username = usermodel.Username, email = usermodel.Email });
}
catch (Exception ae)
{
return View("Index", new LoginOrRegisterModel() { Loginmodel = new LoginModel(), Registermodel = usermodel });
}
}
请帮助我,我收到错误like this
答案 0 :(得分:2)
渲染部分时添加HtmlFieldPrefix:
@Html.Partial("_Register", Model.Registermodel, new ViewDataDictionary(this.ViewData) { TemplateInfo = new TemplateInfo { HtmlFieldPrefix = "Registermodel" } })
@Html.Partial("_Login", Model.Loginmodel, new ViewDataDictionary(this.ViewData) { TemplateInfo = new TemplateInfo { HtmlFieldPrefix = "Loginmodel" } })
或使用PartialFor
在您的操作方法BindAttribute中添加前缀:
public ActionResult Register([Bind(Prefix = "Registermodel")]RegisterModel usermodel)
public ActionResult Login([Bind(Prefix = "Loginmodel")]LoginModel usermodel)