在MVC视图中,我有2个表单,其中包含像bellow
这样多的字段LoginRegisterModel.cs
public class LoginRegisterViewModel {
public string LoginUsername { get; set; }
public string LoginPassword { get; set; }
public string RegisterUsername { get; set; }
public string RegisterPassword { get; set; }
public string RegisterFirstName { get; set; }
public string RegisterLastName { get; set; }
}
LoginRegistte.cshtml(第1版)
@model LoginRegisterViewModel
@using (Html.BeginForm("Login", "MemeberController", FormMethod.Post, new {})) {
@Html.LabelFor(m => m.LoginUsername)
@Html.TextBoxFor(m => m.LoginUsername)
@Html.LabelFor(m => m.LoginPassword)
@Html.TextBoxFor(m => m.LoginPassword)
<input type='Submit' value='Login' />
}
第二种形式
@using (Html.BeginForm("Register", "MemeberController", FormMethod.Post, new {})) {
@Html.LabelFor(m => m.RegisterFirstName)
@Html.TextBoxFor(m => m.RegisterFirstName)
@Html.LabelFor(m => m.RegisterLastName)
@Html.TextBoxFor(m => m.RegisterLastName)
@Html.LabelFor(m => m.RegisterUsername)
@Html.TextBoxFor(m => m.RegisterUsername)
@Html.LabelFor(m => m.RegisterPassword)
@Html.TextBoxFor(m => m.RegisterPassword)
<input type='Submit' value='Register' />
}
MemberController.cs
[HttpGet]
public ActionResult LoginRegister() {
LoginRegisterViewModel model = new LoginRegisterViewModel();
return view("LoginRegister", model);
}
[HttpPost]
public ActionResult Login(LoginRegisterViewModel model) {
if (ModelState.IsValid)
{
//some logic
}
else
{
return view(model);
}
}
[HttpPost]
public ActionResult Register(LoginRegisterViewModel model) {
if (ModelState.IsValid)
{
//some logic
}
else
{
return view(model);
}
}
在这个时候,我直接点击第一个表格按钮,那时我想要第一个表格的火灾验证。但我一次得到两个表格验证。我在控制器或其他地方做了什么?
答案 0 :(得分:0)
试试这个,
<强> Form1中强>
@model LoginRegisterViewModel
@using (Html.BeginForm("Login", "MemeberController", FormMethod.Post, new {id="frmLogin"})) {
@Html.LabelFor(m => m.LoginUsername)
@Html.TextBoxFor(m => m.LoginUsername)
@Html.LabelFor(m => m.LoginPassword)
@Html.TextBoxFor(m => m.LoginPassword)
<input type='Submit' value='Login' id="btnLogin" />
}
<强>窗体2 强>
@using (Html.BeginForm("Register", "MemeberController", FormMethod.Post, new {id="frmRegister"})) {
@Html.LabelFor(m => m.RegisterFirstName)
@Html.TextBoxFor(m => m.RegisterFirstName)
@Html.LabelFor(m => m.RegisterLastName)
@Html.TextBoxFor(m => m.RegisterLastName)
@Html.LabelFor(m => m.RegisterUsername)
@Html.TextBoxFor(m => m.RegisterUsername)
@Html.LabelFor(m => m.RegisterPassword)
@Html.TextBoxFor(m => m.RegisterPassword)
<input type='Submit' value='Register' id= "btnRegister" />
}
<强>脚本强>
$(document).ready(function () {
$('#btnLogin').click(function () {
var validation = $("#frmLogin");
if (!validation.valid()) {
return false;
}
});
$('#btnRegister').click(function () {
var validationRegi = $("#frmRegister");
if (!validationRegi.valid()) {
return false;
}
});
});
答案 1 :(得分:0)
这看起来只是一个错字:
@using (Html.BeginForm("Login", "MemeberController", FormMethod.Post, new {}))
MemeberController应该是MemberController。
它有两个位置,所以一定要抓住它们。