本文介绍了如何根据在MVC下拉菜单选项设置验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的看法code:

 < D​​IV CLASS =表单组-1>
  @ Html.LabelFor(型号=> model.device.HasAMC,htmlAttributes:新{@class =控制标签COL-LG-4})
  @ Html.EditorFor(型号=> model.device.HasAMC,新{htmlAttributes = {新@class =表格控}})
 < / DIV>
 < D​​IV CLASS =表单组1ID =HasDate>
    @ Html.LabelFor(型号=> model.device.AMCExpiredDate,htmlAttributes:新{@class =控制标签COL-LG-4})
    @ Html.EditorFor(型号=> model.device.AMCExpiredDate,新{htmlAttributes = {新@class =表格控,占位符=AMCExpireDate(MM / DD / YYYY),要求=需要,标题=输入AMC失效日期}})
 < / DIV>
<按钮样式=保证金左:33%;类=BTN BTN-SM BTN-主要COL-LG-2型=按钮名称=行动的onclick =JavaScript的:保存(@ ViewBag.DealerId);值=SaveDeviceInfo><强>保存< / STRONG>< /按钮>

在此,我需要设置必填字段AMCExpiredDate一样,如果选择了HasAMC价值真,则所需场申请验证和如果是假的,然后从AMCExpiredDate删除验证。

这是可能的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" } })

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

这篇关于如何根据在MVC下拉菜单选项设置验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 13:02