我是Mvc的新手,我正在创建一个网络应用程序而且我遇到了问题。在我的应用程序中,我有一个注册页面,我需要验证给定的输入,但它不能正常工作我的当前代码如下所示
模型
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>
}
我无法在代码中发现任何错误,因此请指出我的代码错误
答案 0 :(得分:0)
看起来你没有在post方法中传回你的模型。不要将用户名和密码作为字符串传递,而是尝试传递绑定到View的模型类型。您的帖子操作方法签名应如下所示:
public ActionResult SignUp(AccountModel model)