如何使用多个操作显示多个表单,但仅验证其中一个

时间:2015-06-11 21:33:14

标签: c# asp.net-mvc razor

我的UI设计中有一个设计要求我在页面上有3个特定部分:

我的页面上有3个表单元素,3个按钮分别用于不同的操作。但是当我提交一个特定的表格时,整个页面都会验证,而不仅仅是我试图验证的表格。

是否有人知道如何使其仅验证我提交的表单? 我唯一能想到的是删除模型验证并通过jquery / javascript验证这一点

以下是模型:

using System.ComponentModel.DataAnnotations;

namespace Project.Models
{
    public class SecurityModel
    {
        public string UserName { get; set; }

        [Required]
        [DataType(DataType.Password)]
        [StringLength(15, MinimumLength = 8)]
        public string Password { get; set; }

        [Required]
        [DataType(DataType.Password)]
        [RegularExpression("^(?=.{8,15}$)(?=(.*[A-Z]+))(?=(.*[a-z]*))(?=(.*[0-9]){2,}).*$")]
        [StringLength(15, MinimumLength = 8)]
        public string NewPassword { get; set; }

        [Required]
        [Compare("NewPassword")]
        [DataType(DataType.Password)]
        [StringLength(15, MinimumLength = 8)]
        public string NewPassword2 { get; set; }

        public string EncryptionKey { get; set; }
    }
} 

这是视图

@using System.Collections
@using Project.Models
@model SecurityModel
@{
    ViewBag.Title = "ChangePassword";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="settings">
    <h3 class="nopadding">Change your password</h3>
    <div class="formblock">
        <label>For security reasons a new password is required.</label>
        @using (@Html.BeginForm("Settings", "Security", FormMethod.Post, new { id = "Settings" }))
        {


            @Html.DisplayFor(user => user.UserName)@Html.HiddenFor(user => user.UserName)
            @Html.PasswordFor(user => user.Password, new { @placeholder = "Password", @maxlength = "15", @tabindex = "1" })@Html.ValidationMessageFor(user => user.Password)<br />
            @Html.PasswordFor(user => user.NewPassword, new { @placeholder = "New Password", @maxlength = "15", @tabindex = "2" })@Html.ValidationMessageFor(user => user.NewPassword)<br />
            <p class="fieldhint">8-15 characters; two numbers and one uppercase required.</p>
            @Html.PasswordFor(user => user.NewPassword2, new { @placeholder = "New Password", @maxlength = "15", @tabindex = "3" })@Html.ValidationMessageFor(user => user.NewPassword2)<br />
            <button class="blue-large-button" tabindex="4" type="submit">Save</button>
        }
    </div>
</div>
<div class="settings">
    <h3 class="nopadding">Change service data encryption key.</h3>
    <div class="formblock">
        <label>Stuff</label>
        @using (@Html.BeginForm("ChangeEncryptionKey", "Security", FormMethod.Post, new {id="ChangeEncryptionKey"}))
        {
            @Html.TextBoxFor(security => security.EncryptionKey, new { @placeholder = "Encryption Key", @tabindex = "1" })@Html.ValidationMessageFor(security => security.EncryptionKey)<br />
            <button class="blue-large-button" tabindex="4" type="submit">Save</button>
        }
    </div>
</div>
<div class="settings">
    <h3 class="nopadding">Get service data encryption key.</h3>
    <div class="formblock">
        <label>Yep</label>
        @using (@Html.BeginForm("GetEncryptionKey", "Security", FormMethod.Post, new { id = "GetEncryptionKey" }))
        {
            <button class="blue-large-button" tabindex="4" type="submit">Save</button>
        }
    </div>
</div>

0 个答案:

没有答案
相关问题