【问题标题】:how to set validation according dropdown selection in MVC?如何根据 MVC 中的下拉选择设置验证?
【发布时间】:2016-05-16 10:51:55
【问题描述】:

这是我的查看代码:

<div class="form-group-1">
  @Html.LabelFor(model => model.device.HasAMC, htmlAttributes: new { @class = "control-label col-lg-4" })
  @Html.EditorFor(model => model.device.HasAMC, new { htmlAttributes = new { @class = "form-control" } })
 </div>
 <div class="form-group-1" id="HasDate">
    @Html.LabelFor(model => model.device.AMCExpiredDate, htmlAttributes: new { @class = "control-label col-lg-4" })
    @Html.EditorFor(model => model.device.AMCExpiredDate, new { htmlAttributes = new { @class = "form-control", placeholder = "AMCExpireDate(MM/dd/yyyy)", required = "required", title = "Enter AMC Expire Date" } })
 </div>
<button style="margin-left:33%;" class="btn btn-sm btn-primary col-lg-2 " type="button" name="action" onclick="javascript:Save(@ViewBag.DealerId);" value="SaveDeviceInfo"><strong>Save</strong></button>

在此我需要设置必填字段“AMCExpiredDate”,例如“HasAMC”的值是否选择“true”,然后对必填字段应用验证,如果为假,则从“AMCExpiredDate”中删除验证。

这在 MVC 中可能吗? 请帮我。提前致谢。

【问题讨论】:

标签: jquery asp.net-mvc validation


【解决方案1】:

将您的视图更改为类似的内容

 <div class="form-group-1">
      @Html.LabelFor(model => model.device.HasAMC, htmlAttributes: new { @class = "control-label col-lg-4" })
      @Html.EditorFor(model => model.device.HasAMC, new { htmlAttributes = new { @class = "form-control" } })
     </div>
     <div class="form-group-1" id="HasDate">
        @Html.LabelFor(model => model.device.AMCExpiredDate, htmlAttributes: new { @class = "control-label col-lg-4" })
        @Html.EditorFor(model => model.device.AMCExpiredDate, new { htmlAttributes = new { @class = "form-control", placeholder = "AMCExpireDate(MM/dd/yyyy)", required = "required", title = "Enter AMC Expire Date" } })

        // put validation-error  
      @Html.ValidationMessageFor(model => model.device.AMCExpiredDate);
                                    @if (@ViewData.ModelState["AMCExpiredDate"] != null && @ViewData.ModelState["AMCExpiredDate"].Errors.Count > 0)
                                    {
                                        <br/>
                                        <span class="field-validation-error col-md-10" style="margin-left: -14px;">
                                        @ViewData.ModelState["AMCExpiredDate"].Errors[0].ErrorMessage.Trim()
                                    </span>
                                    }

     </div>
    <button style="margin-left:33%;" class="btn btn-sm btn-primary col-lg-2 " type="button" name="action" onclick="javascript:Save(@ViewBag.DealerId);" value="SaveDeviceInfo"><strong>Save</strong></button>


            // create hidden field of your model variables
                 @Html.HiddenFor(modelItem => model.device.HasAMC)
                 @Html.HiddenFor(modelItem => model.device.HasAMC)

在服务器端

public ActionResult SaveMarks(mymodelClass model)

            if (model.device.HasAMC)
            {
               // validat you AMCExpiredDate expir date e.georgian
               if(model.device.AMCExpiredDate == null)
               {
                ModelState.AddModelError("AMCExpiredDate", "Please Proive you AMCExpiredDate");
               }
            }

【讨论】:

  • 感谢您的回答。但我只需要在客户端而不是服务器端。
  • 那么问题不大,您只需在按钮函数“onclick="javascript:Save(@ViewBag.DealerId);”后面添加一个验证逻辑,例如代码 if($('#HasAMC ').val() === true) { // 验证日期 }
猜你喜欢
  • 2014-11-19
  • 1970-01-01
  • 2017-11-12
  • 2021-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多