【发布时间】:2014-10-12 11:25:33
【问题描述】:
我使用 MVC Razor 创建了一个登录页面。该页面包含要输入的用户名和密码以及两个按钮登录和注册按钮。
我已经为两个文本框应用了必填字段验证。当我单击登录按钮时,它工作正常。对于注册按钮,我希望将页面重定向到注册页面。但是当我点击注册按钮时,它会抛出错误,因为需要用户名和密码。
仅单击登录按钮时如何检查验证。
查看页面:
<body>
@using (Html.BeginForm("CheckLogin", "Login"))
{
<div>
<div>
<img src="@Url.Content("../../Content/Images/Logo.png")" alt="logo" style="margin-top:17px;" />@*width="184" height="171"*@
</div>
<div class="LoginBg">
<h3 class="LoginHdng">
@Html.Label("lblLogin", "Login")
</h3>
<table style="width: 745px;">
<tr>
<td>
<br />
</td>
</tr>
<tr>
<td style="font-size: 20px; text-align: right; width: 250px">
@Html.LabelFor(model => model.FirstName, "User Name: ")
</td>
<td>
@Html.TextBoxFor(model => model.FirstName, new { @class = "txtBox" })
@Html.ValidationMessageFor(model => model.FirstName)
</td>
</tr>
<tr>
<td>
<br />
</td>
</tr>
<tr>
<td style="font-size: 20px; text-align: right;">
@Html.LabelFor(model => model.Password, "Password: ")
</td>
<td>
@Html.PasswordFor(model => model.Password, new { @class = "txtBox" })
@Html.ValidationMessageFor(model => model.Password)
</td>
</tr>
<tr>
<td colspan="2" style="font-size: 16px; text-align: center; color: Red;">
<br />
@TempData["Error"]
</td>
</tr>
<tr>
<td>
<br />
</td>
</tr>
<tr>
<td colspan="2" style="text-align: center;">
<input type="submit" value="Login" name="Command" class="loginBtn" />
<input type="submit" value="SignUp" name="Command" class="loginBtn" />
</td>
</tr>
</table>
</div>
</div>
}
</body>
控制器:
[HttpPost]
public ActionResult CheckLogin(FormCollection coll)//, string command
{
if (coll["Command"] == "Login")
{
using (var db = new MVC_DBEntities())
{
if (db.tbl_UserDetails.ToList().Select(m => m.FirstName == coll["FirstName"] && m.Password == coll["Password"]).FirstOrDefault())
{
TempData["Error"] = "";
return this.RedirectToAction("Index", "Home");
}
else
{
TempData["Error"] = "Invalid UserName/Password";
return this.RedirectToAction("Login", "Login");
}
}
}
else if (coll["Command"] == "SignUp")
{
return this.RedirectToAction("SignUp", "SignUp");
}
else
{
return this.RedirectToAction("Login", "Login");
}
}
在此先感谢...
【问题讨论】:
-
尝试将调试点放在 else if (coll["Command"] == "SignUp") 并检查其到达是否听到?
标签: jquery asp.net asp.net-mvc asp.net-mvc-3 razor