如何根据在MVC下拉菜单选项设置验证? [英] how to set validation according dropdown selection in MVC?

查看:101
本文介绍了如何根据在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?
请帮帮我。先谢谢了。


解决方案

  

更改您的视图像这样


 < 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失效日期}})        //把验证错误
      @ Html.ValidationMessageFor(型号=> model.device.AMCExpiredDate);
                                    @if(@ ViewData.ModelState [AMCExpiredDate] = NULL&放大器;!&安培; @ ViewData.ModelState [AMCExpiredDate] Errors.Count方式> 0)
                                    {
                                        < BR />
                                        <跨度类=现场验证错误COL-MD-10的风格=保证金左:-14px;>
                                        @ ViewData.ModelState [AMCExpiredDate]。错误[0] .ErrorMessage.Trim()
                                    < / SPAN>
                                    }     < / DIV>
    <按钮样式=保证金左:33%;类=BTN BTN-SM BTN-主要COL-LG-2型=按钮名称=行动的onclick =JavaScript的:保存(@ ViewBag.DealerId);值=SaveDeviceInfo><强>保存< / STRONG>< /按钮>
            //创建模型变量的隐藏字段
                 @ Html.HiddenFor(modelItem => model.device.HasAMC)
                 @ Html.HiddenFor(modelItem => model.device.HasAMC)


  

在服务器端


 公众的ActionResult SaveMarks(mymodelClass模型)            如果(model.device.HasAMC)
            {
               // validat你AMCExpiredDate expir日期e.georgian
               如果(model.device.AMCExpiredDate == NULL)
               {
                ModelState.AddModelError(AMCExpiredDate,请你Proive AMCExpiredDate);
               }
            }

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆