简体   繁体   中英

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

Is this possible in 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");
               }
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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