简体   繁体   English

验证在MVC4中不起作用

[英]Validation Not Working in MVC4

I'm new to Mvc and I'm creating a web app and I'm stuck with a problem. 我是Mvc的新手,正在创建一个Web应用程序,但遇到了问题。 In my app I've a signup page and i need to validate the given input but its not working my current code is given below 在我的应用程序中,我有一个注册页面,我需要验证给定的输入,但是它不起作用,下面给出了当前代码

Model 模型

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; }

}

Controller 控制者

[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();
        }

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

I cant find anything wrong in my code so please point out whats wrong with my code 我在代码中找不到任何错误,因此请指出我的代码有什么问题

Looks like you're not passing your model back in the post method. 看起来您没有在post方法中传递模型。 Instead of passing the username and passwords as strings, try passing the model type that you bound to the View. 不要将用户名和密码作为字符串传递,而是尝试传递绑定到View的模型类型。 Your post action method signature should look something like this: 您的后操作方法签名应如下所示:

public ActionResult SignUp(AccountModel model)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM