0

我是 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>
    }

我在我的代码中找不到任何问题,所以请指出我的代码有什么问题

4

1 回答 1

0

看起来您没有在 post 方法中将模型传回。不要将用户名和密码作为字符串传递,而是尝试传递绑定到视图的模型类型。您的 post 操作方法签名应如下所示:

public ActionResult SignUp(AccountModel model)
于 2013-05-14T07:23:32.857 回答