要么在剃刀MVC3中进行验证



我有一个带有几个文本框的剃须刀视图。

我想让你选择其中一个文本框或另外 3 个文本框,除非您在其中至少一个选项中输入文本,否则您无法提交......IE 您可以输入社会安全号码,也可以输入人的名字、姓氏和出生日期。 对于最后一个选项,您必须输入名字、姓氏和 DOB。 使用不显眼的验证可以做到这一点吗?以下是我的分会

<div id="searchByDemographicsDiv"style="float: left;width: 50%; display:inline; ">
            @*@using(Html.BeginForm("SearchByDemographic", "SearchPatients",FormMethod.Post, new{ id = "searchByDemographics"})){*@
            @using (Ajax.BeginForm("SearchByDemographic", "SearchPatients", null, new AjaxOptions { HttpMethod = "POST", LoadingElementId = Url.Content("~/Images/ajax-loader.gif"), OnSuccess = "binddata", OnFailure = "FailAlert" }, new { id = "searchByDemographics" })){ 
          <ul>
                      <li>@Html.Label("SSN", new { @for = "SSN" })  <br />@Html.TextBox("SSN", null, new { id = "SSN", name = "SSN", @class = "SSN" })</li>
          </ul>
          <ul>
          <p>Or</p>
          </ul>
          <ul>
            <li>@Html.Label("FirstName", new { @for = "FirstName" }) <br />@Html.TextBox("FirstName", null, new { id = "FirstName", @class = "demoGrphcs", name = "FirstName" })</li>
            <li>@Html.Label("LastName", new { @for = "LastName" }) <br />@Html.TextBox("LastName", null, new { id = "LastName", @class = "demoGrphcs", name = "LastName" })</li>
            <li>@Html.Label("dateOfBirth", new { @for = "dateOfBirth" }) <br />@Html.TextBox("dateOfBirth", null, new { id = "dateOfBirth", @class = "demoGrphcs", name = "dateOfBirth" })</li>
            <li>@Html.Label("Address1", new { @for = "Address1" }) <br />@Html.TextBox("Address1", null, new { id = "Address1" })</li>
            <li>@Html.Label("Address2", new { @for = "Address2" }) <br />@Html.TextBox("Address2", null, new { id = "Address2" })</li>
            <li>@Html.Label("City", new { @for = "City" }) <br />@Html.TextBox("City", null, new { id = "City" })</li>
            <li>@Html.Label("State", new { @for = "State" }) <br />@Html.TextBox("State", null, new { id = "State" })</li>
            <li>@Html.Label("Country", new { @for = "Country" }) <br />@Html.TextBox("Country", null, new { id = "Country" })</li>
            <li>@Html.Label("PostCode", new { @for = "PostCode" }) <br />@Html.TextBox("PostCode", null, new { id = "PostCode" })</li>
          </ul>  
        <input type="submit" value="Search By Demographics" class="button" id="submitDemo"/>     
            }
</div>

我基本上希望用户能够按SSN或其他人口统计信息进行搜索,其中名字姓氏和出生日期是必填字段。 如果按SSN搜索,我希望这是必填字段。 我知道非常复杂。

已更新**

对我的表单元素进行原始验证**更新

$("#searchByDemographics").validate({
    rules: {
        SSN: {
            require_from_group: [1, ".SSN"]
        },
        FirstName: {
            require_from_group: [1, ".demoGrphcs"]
        },
        LastName: {
            require_from_group: [1, ".demoGrphcs"]
        },
        DateOfBirth: {
            require_from_group: [1, ".demoGrphcs"]
        }
    },
    messages : {
        SSN: 'Please Enter either a valid SSN or Demographic Info below',
        FirstName: 'First name is required',
        LastName: 'Last name is required',
        DateOfBirth: 'Date of Birth is required'
    }
});

我认为简单的方法是从模型中传递所有这些值并在 POST 上检查它,根据您的要求进行验证。

我不确定为什么在这种情况下要进行不显眼的验证,因为验证非常具体。当您尝试完成的验证是可重用的时,不显眼的验证很有意义,但事实并非如此。

我建议您在服务器端验证模型中实现IValidatableObject,并使用普通 jquery 进行客户端验证。

最新更新