Asp.net MVC 5表单值在post方法和调用操作之前进行验证

时间:2014-07-14 18:24:37

标签: asp.net-mvc-5

我有一个视图,有弹出框与form.And我正在做的是从弹出窗口我显示用户信息,他可以编辑信息。所以这里我想应用验证,如果验证失败,需要在弹出窗口显示错误。

我尝试了,但是当我提交表单时,它提交值并且如果验证失败则进入控制器并执行操作。 比我的视图再次加载,我正在丢失弹出窗口。

我怎样才能实现这一点,任何人都可以帮助我, 提前致谢

@using (@Html.BeginForm("AdminUpdate", "Images", FormMethod.Post,new {id="updateform", enctype = "multipart/form-data" }))
           {
                <table style="margin:auto;">
                    <tr>
                        <td colspan="2">
                            <label id="myfirstname" style="color:#336699;"></label>&nbsp;
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label>Profile Picture:</label>
                        </td>
                        <td>
                            <img src="@Url.Content("~/Content/Images/" + item.EmailId + item.Picture)" alt="profileimage" style="height:80px;width:80px;" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label>First Name:</label>
                        </td>
                        <td style="width:10px;">
                            @Html.TextBox("firstname", item.FirstName)
                        </td>



                    </tr>
                    <tr>
                        <td>
                            <label>Last Name:</label>
                        </td>
                        <td>
                            @Html.TextBox("lasttname", item.LastName)

                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label>Website:</label>
                        </td>
                        <td>
                            @Html.TextBox("website", item.Website)

                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label>Email Id:</label>
                        </td>
                        <td>
                            @Html.TextBox("emailid", item.EmailId)

                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label>Mobile:</label>
                        </td>
                        <td>
                            @Html.TextBox("mobile", item.Phone)

                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label>Address:</label>
                        </td>
                        <td>
                            @Html.TextBox("address", item.Address)

                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label>New Password</label>
                        </td>
                        <td>
                            @Html.Password("newpassword")
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label>Profile Picture:</label>
                        </td>
                        <td>
                            <input type="file" name="newpic" />
                        </td>
                    </tr>
                    <tr>
                        <td style="padding-top:10px;">
                           &nbsp;
                        </td >
                        <td style="padding-top:10px;">
                            <input type="submit" class="form-submit" style="cursor:pointer" name="updatesubmit" value="Update" />
                        </td>                            
                    </tr>
                    <tr>
                        <td>
                            @Html.TextBox("aid", ViewContext.RouteData.Values["id"], new { style = "display:none;" })
                        </td>
                        <td>
                            @Html.TextBox("id", item.id, new { style = "display:none;" })
                        </td>
                    </tr>
                </table>
               <label style="color:red;padding-top:6px;padding-bottom:3px;">*If you didn't provide picture and password,old values will be used</label>
           }                        <tr>
                        <td>
                            <label>First Name:</label>
                        </td>
                        <td>
                            @Html.TextBox("firstname", item.FirstName)
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label>Last Name:</label>
                        </td>
                        <td>
                            @Html.TextBox("lasttname", item.LastName)
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label>Website:</label>
                        </td>
                        <td>
                            @Html.TextBox("website", item.Website)
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label>Email Id:</label>
                        </td>
                        <td>
                            @Html.TextBox("emailid", item.EmailId)
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label>Mobile:</label>
                        </td>
                        <td>
                            @Html.TextBox("mobile", item.Phone)
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label>Address:</label>
                        </td>
                        <td>
                            @Html.TextBox("address", item.Address)
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label>New Password</label>
                        </td>
                        <td>
                            @Html.Password("newpassword")
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label>Profile Picture:</label>
                        </td>
                        <td>
                            <input type="file" name="newpic" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            &nbsp;
                        </td>
                    </tr>
                    <tr>
                        <td>
                            @Html.TextBox("aid", ViewContext.RouteData.Values["id"], new { style = "display:none;" })
                            @Html.TextBox("id", item.id, new { style = "display:none;" })
                            <input type="submit" class="form-submit" name="updatesubmit" value="Update" />
                        </td>
                        <td>
                            <a id="close"> close</a>
                        </td>
                    </tr>
                </table>
           }

2 个答案:

答案 0 :(得分:0)

$('#updateform').submit(function () {
    if ($("#firstname").val() == "" || $("#lasttname").val() == "" || $("#website").val() == "" || $("#emailid").val() == "" || $("#mobile").val() == "" || $("#address").val() == "") 
    {
        $("#myfirstname").text("*Please enter details");
        return false;
    }
    else if (!($("#firstname").val().match("^([a-zA-Z]+)?$")))
    {
        $("#myfirstname").text("*Invalid first name format");
        return false;

    }
    else if (!($("#lasttname").val().match("^([a-zA-Z]+)?$")))
    {
        $("#myfirstname").text("*Invalid last name format");
        return false;

    }
    else if (!($("#website").val().match("^((https?|ftp)://|(www|ftp)\\.)[a-z0-9-]+(\\.[a-z0-9-]+)+([/?].*)?$")))
    {
        $("#myfirstname").text("*Invalid website format");
        return false;

    }
    else if (!($("#emailid").val().match("^[_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)$")))
    {
        $("#myfirstname").text("*Invalid emailid format");
        return false;

    }
    else if (!($("#mobile").val().match("^\\(?([7-9]{1})\\)?([0-9]{9})$")))
    {
        $("#myfirstname").text("*Invalid mobile number");
        return false;

    }
    else if (!($("#address").val().match("^([a-zA-Z]+)?$")))
    {
        $("#myfirstname").text("*Invalid address");
        return false;

    }
    else if ($("#newpassword").val()!="")
    {
        if ($("#newpassword").val().length < 6 || $("#newpassword").val().length>10)
        {
            $("#myfirstname").text("*Password lenght min 6chars and max 10chars");
            return false;
        }
    }
    else
    {
        return true;
    }
});

答案 1 :(得分:0)

好的..我们可以在MVC应用程序中使用jquery做很多事情,但是如果用户禁用了JavaScript,则此验证将失败。为了克服这个问题,我们可以使用MVC5的 DataAnnotations 功能。在这种情况下, ModelState.IsValid 将为false;我们可以按照要求在控制器中处理。您可以按照以下简单方式验证数据:

模型

using System.ComponentModel.DataAnnotations;

[Required(ErrorMessage = "Please enter Name")]
[StringLength(50, ErrorMessage = "Name can not be more than 50 characters ")]
[DisplayName("Name")]
public string name { get; set; }

[Required(ErrorMessage = "Please enter Date of Birth")]
[DisplayName("Date of Birth")]
[DataType(DataType.Date,ErrorMessage = "Invalid Date ")]
public DateTime dob { get; set; }

控制器

    [HttpPost]
    public ActionResult Registration(RegistrationClass obj)
    {
    if(ModelState.IsValid)
    {
       // Code if Data is valid , may be insert data to database
    }
    else 
    {
    obj = obj.getAllDropdown();
    }
    return View(obj);
}

getAllDropdown 是我用户定义的函数,用于填充下拉列表。