2016-05-16 8 views
0

Hier ist meine Ansicht Code:Wie setze ich die Validierung nach Dropdown-Auswahl in MVC?

<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 diesem I erforderliches Feld „AMCExpiredDate“ festlegen müssen, wie wenn der Wert von „HasAMC“ ausgewählt „true“ dann Validierung für erforderliches Feld anwenden und wenn es falsch ist, dann entfernen Validierung von "AMCExpiredDate".

Ist dies in MVC möglich? Bitte helfen Sie mir. Danke im Voraus.

+1

eine [narrensicher] Betrachten (http://foolproof.codeplex.com /) '[RequiredIf]' oder ähnliches Validierungsattribut (oder Sie können Ihr eigenes schreiben) –

+0

Mögliches Duplikat von [Conditio nale Validierung auf Modell in MVC] (http://stackoverflow.com/questions/17970584/conditional-validation-on-model-in-mvc) – Pete

+0

tun Sie es einfach mit jquery, bei onchange-Ereignis, fügen Sie das Validierungsattribut zum Dropdown hinzu –

Antwort

1

Ändern Sie Ihre Ansicht zu so etwas wie dieses

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

auf Server-Seite

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

Danke für die Antwort. Aber ich brauche das auf der Client-Seite nur nicht Server-Seite. –

+0

Dann ist es nicht das große Problem, was Sie brauchen, ist eine Validierungslogik hinter Button-Funktion "onclick =" javascript: Speichern (@ ViewBag.DealerId); " code if ($ ('# HasAMC'). val() === true) { // Überprüfen Sie das Datum } –

Verwandte Themen