简体   繁体   English

如何在MVC中根据下拉选择设置验证?

[英]how to set validation according dropdown selection in MVC?

Here is my View code: 这是我的查看代码:

<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>

In this I need to set required field "AMCExpiredDate" like if value of "HasAMC" selected "true" then apply validation for required field and if it is false then remove validation from "AMCExpiredDate". 在这种情况下,我需要设置必填字段“ AMCExpiredDate”,例如,如果“ HasAMC”的值选择为“ true”,则对必填字段应用验证,如果为false,则从“ AMCExpiredDate”中删除验证。

Is this possible in MVC? 在MVC中这可能吗? Please Help me. 请帮我。 Thanks in Advance. 提前致谢。

Change your view to something like this 将您的视图更改为这样

 <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)

on Server Side 在服务器端

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");
               }
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM