一个视图中的多个模型 - 使用 ViewModel 登录和注册 - 具有字段和模型状态验证



我有一个MVC 5应用程序,在一个视图中使用多个模型登录&登记。谷歌搜索&Stack Overflow返回许多关于如何实现此功能的示例。这个例子部分有效。然而,我有一个问题与验证,我似乎找不到一个明确的例子。如果我单击登录表单而没有在字段中输入任何值,我总是得到一个空引用,并且我的ModelState总是有效的!

My Login model:

public class LoginModel
{
[Required(ErrorMessage = "Please enter a valid username.")]
public string txtUsername { get; set; }
[Required(ErrorMessage = "The password field is required.")]
public string txtPassword { get; set; }
}

My Registration model:

public class RegisterModel
{
[Required(ErrorMessage = "Please enter a first name.")]
public string txtFirstName { get; set; }
[Required(ErrorMessage = "Please enter a last name.")]
public string txtLastName { get; set; }
}

我的视图模型:

public class ViewModelVM
{
public LoginModel loginModel { get; set; }
public RegisterModel registerModel { get; set; }
}

My Home Controller submit and register actions:

[HttpPost]
public ActionResult LoginSubmit(ViewModelVM vm)
{
if (ModelState.IsValid)
{
//perform other logic. 
return View("Index");
}
else
{
return View("Login", vm.loginModel);
}
}
[HttpPost]
public ActionResult RegisterSubmit(ViewModelVM vm)
{
if (ModelState.IsValid)
{ 
return View("Index");
}
else
{
return View("Login", vm);
}
}

我的观点:

@model MVCTemplate.ViewModel.ViewModelVM
@{
ViewBag.Title = "Login & Register";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="col-lg-10 mx-auto">
@using (Html.BeginForm("LoginSubmit", "Home", FormMethod.Post))
{
<div id="loginForm" class="p-5">
<div class="form-group">
@Html.Label("Username", new { @class = "labeltiny" })
@Html.TextBox("txtUserName", "", new { @class = "form-control", @PlaceHolder = "Username" })
@Html.ValidationMessageFor(x => x.loginModel.txtUsername, "", new { @class = "text-danger labeltiny" })
</div>
<div class="form-group">
@Html.Label("Password", new { @class = "labeltiny" })
@Html.Password("txtPassword", "", new { @class = "form-control", @PlaceHolder = "Password" })
@Html.ValidationMessageFor(x => x.loginModel.txtPassword, "", new { @class = "text-danger labeltiny" })
</div>
<div class="form-group">
<div class="row">
<div class="col-md-12 text-center">
<input type="submit" name="btnLogin" id="btnLogin" tabindex="4" class="form-control btn btn-login" value="Log In" onclick="sweetalertSpinner()">
</div>
</div>
</div>
</div>
}
@using (Html.BeginForm("RegisterSubmit", "Home", FormMethod.Post))
{
<div id="registerForm" class="p-5" style="display: none">
<div class="form-group">
@Html.Label("First Name", new { @class = "labeltiny" })
@Html.TextBox("txtFirstName", "", new { @class = "form-control", @PlaceHolder = "Fist Name" })
@Html.ValidationMessageFor(x => x.registerModel.txtFirstName, "", new { @class = "text-danger labeltiny" })
</div>
<div class="form-group">
@Html.Label("Last Name", new { @class = "labeltiny" })
@Html.Password("txtLastName", "", new { @class = "form-control", @PlaceHolder = "Last Name" })
@Html.ValidationMessageFor(x => x.registerModel.txtLastName, "", new { @class = "text-danger labeltiny" })
</div>
<div class="form-group">
<div class="row">
<div class="col-md-12 text-center">
<input type="submit" name="btnRegister" id="btnRegister" tabindex="4" class="form-control btn btn-login" value="Register" onclick="sweetalertSpinner()">
</div>
</div>
</div>
</div>
}
</div>

正如文档所说:"模型状态表示来自两个子系统的错误:模型绑定和模型验证。">

ModelState将只表示一个模型,并且由于您正在定义视图模型

public class ViewModelVM
{
public LoginModel loginModel { get; set; }
public RegisterModel registerModel { get; set; }
}

那么模型绑定和模型验证将基于ViewModelVM的属性,而不是其对象中的属性,这就是为什么你会得到ModelState.IsValid == true

我认为您可以使用的是每个模型的手动验证,在这里您可以找到这样做的方法。

也许你可以创建一个类,从你想要的两个模型继承,但我不确定这是否可以工作。如果你试过了,请告诉我们。

我找到了解决方案。将以下内容添加到ViewModel

public class ViewModelVM
{
public LoginModel loginModel { get; set; }
public RegisterModel registerModel { get; set; }
public string txtUsername { get; set; }
public string txtPassword { get; set; }
public string txtFirstName { get; set; }
public string txtLastName { get; set; }
}

按如下方式更改每个表单字段的View(删除对loginModelregisterModel的引用):

@Html.ValidationMessageFor(x => x.txtUsername, "", new { @class = "text-danger labeltiny" })

在控制器中传递LoginModel作为参数,而不是ViewModelVM,就像这样(对RegisterSubmit做同样的事情):

[HttpPost]
public ActionResult LoginSubmit(LoginModel lm)
{
ViewModelVM vm = new ViewModelVM();
if (ModelState.IsValid)
{
//perform other logic using form fields lm.txtUsername, etc... 
return View("Index");
}
else
{
return View("Login", vm);
}
}

相关内容

  • 没有找到相关文章