验证不工作在MVC4
本文关键字:MVC4 工作 验证 | 更新日期: 2023-09-27 18:13:19
我是Mvc的新手,我正在创建一个web应用程序,我遇到了一个问题。在我的应用程序,我有一个注册页面,我需要验证给定的输入,但它不工作,我目前的代码是下面给出
模型public class Account
{
[Required(ErrorMessage = "User Name is required")]
[StringLength(15, ErrorMessage = "First Name length Should be less than 50")]
public virtual string UserName { get; set; }
[Required(ErrorMessage = "Email Id is required")]
[StringLength(35, ErrorMessage = "eMail Length Should be less than 35")]
[RegularExpression(@"^'w+@[a-zA-Z_]+?'.[a-zA-Z]{2,3}$", ErrorMessage = "Email Id is not in proper format")]
public virtual string EmailId { get; set; }
}
控制器[HttpPost]
public ActionResult SignUp(string userName,string email)
{
if (ModelState.IsValid)
{
try
{
Account newAccount = new Account();
var userExist = newAccount.UserExist(userName);
if (userExist == 0)
{
AccountBL createAccount = new AccountBL();
createAccount.UserName = userName;
createAccount.EmailId = email;;
newAccount.SignUp(createAccount);
return View("Index");
}
else
{
return View();
}
}
catch
{
return View();
}
}
return View();
}
视图 @using (Html.BeginForm("SignUp", "Account", FormMethod.Post))
{
@Html.ValidationSummary(true)
<div>
<fieldset>
<legend>Sign Up</legend>
<table>
<tr>
<td>
@Html.Label("User Name")
</td>
<td>
@Html.TextBoxFor(account => account.UserName)
@Html.ValidationMessageFor(account => account.UserName)
</tr>
<tr>
<td>
@Html.Label("Email")
</td>
<td>
@Html.TextBoxFor(account => account.EmailId)
@Html.ValidationMessageFor(account => account.EmailId)
</td>
</tr>
<tr>
<td>
<input type="submit" name="btnSubmit" value="Sign Up" />
</td>
</tr>
</table>
</fieldset>
</div>
}
我找不到我的代码有什么问题,所以请指出我的代码有什么问题
看起来你没有在post方法中传递你的模型。不要以字符串的形式传递用户名和密码,而是尝试传递绑定到视图的模型类型。你的post - action方法签名应该看起来像这样:
public ActionResult SignUp(AccountModel model)