如何添加模型状态错误并在视图页面中显示?

时间:2017-12-31 10:21:29

标签: html asp.net-mvc-4

如果用户没有填写所有文本框并单击“提交”按钮,那么如何添加模型状态错误并在视图页面中显示(或)如何在视图页面中显示此错误(所有字段都是必填字段)。我没有在asp.net上有很好的知识,我最近学到了这个,所以请帮助我。对我来说应该很难......感谢你



 

    @using StackApplication.Modelsl;



<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Register Page</title>
</head>

<body>
    <div class="header">
        <div id="title">
            <font color="blue">  <b> <h1>STaCK</h1> </b> </font>
        </div>
        <div class="sub">
            <a href="" style="color:white;
                padding:10px;">Log in</a>
            <a href="" style="color:white;">Sign up</a>
        </div>
    </div>


    <div style="float:left;width:100%;padding:left:10px;top:5px;">
        <center>
            <b><i>  <h2><font color="#010100">Register Form</font></h2> </i></b>
        </center>
    </div>

    <div id="content">
        <center>
            @using (Html.BeginForm(FormMethod.Post))
            {
                <table cellpadding="5" cellspacing="10">
                    <tr>
                        <th><h3>Username</h3></th>
                        <td>
                            @Html.TextBoxFor(m=>m.UserAccount.UserName, new { @placeholder = "Username", @id = "txtUsername", @required="required" })
                        </td>
                    </tr>
                    <tr>
                        <th><h3>Email id</h3></th>
                        <td>

                           @Html.TextBoxFor(m => m.UserAccount.Email, new { @placeholder = "Email", @id = "txtEmail", @required = "required" })

                            
                        </td>
                    </tr>
                    <tr>
                        <th><h3>Password</h3></th>
                        <td>
                           
                           @Html.TextBoxFor(m => m.UserAccount.Password, new { @placeholder = "Password", @id = "txtPassword", @required = "required", @type="password" })          
                            
                             
                       </td>
                    </tr>

                </table>

                <input type="submit" name="register" value="REGISTER" id="buttondesign" />
                <br />
                <br />
            }
    </div>
</body>
</html>
    
&#13;
&#13;
&#13;

public class StackProvider
{
    public object ModelState { get; private set; }

    public string CreateUserAccount(UserAccount userAccount)
    {
        try
        {
            StackRepository repository = new StackRepository();

            if (string.IsNullOrEmpty(userAccount.Email) || string.IsNullOrEmpty(userAccount.Password)
                || string.IsNullOrEmpty(userAccount.UserName))
            {
                return "All fields are mandatory.";
            }

            int count = repository.GetUseraccountByEmail(userAccount.Email);

            if (count > 0)
            {
                ModelState.AddModelError("Username already exist");
                return "Username already exist";
            }

            repository.CreateUserAccount(userAccount);

            return "Success";
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }

1 个答案:

答案 0 :(得分:0)

如果我对属性或模型进行自定义验证,我确实喜欢这个,我看了你的问题,所以尝试这可以帮助你提交表格和...

=&gt;执行结束时 ModelState Object 将包含为验证失败而添加的所有错误。此外, modelState对象将包含错误消息。

=&gt;因为我们检查IsValid属性的值并且它被设置为false ,所以我们再次将用户重定向到寄存器视图以填充字段。

           public ActionResult Register()
           {
             return View();


        }
      [HttpPost]
     public ActionResult Register( Register registermodel)
        //Here we are checking wether input field are null or Empty

    //if fields are Null/Empty ,we are  adding error in ModelState object against those properties
    {
        if (string.IsNullOrEmpty(registermodel.UserName))
        {
            ModelState.AddModelError("UserName","UserName is required");
        }
        if (string.IsNullOrEmpty(registermodel.email))
        {
            ModelState.AddModelError("email ", "email is required");
        }
        if (string.IsNullOrEmpty(registermodel.Password))
        {
            ModelState.AddModelError("Password ", "Password is required");
        }



        //Here,if All the validation are passed then redirect to index page.
        if (ModelState.IsValid)
        {

            return RedirectToAction("Index","Home");
        }

        //if we got this far,something failed,redisplay form
        return View(registermodel);
    }