Jquery 如何在MVC中根据下拉选择设置验证?
以下是我的查看代码:Jquery 如何在MVC中根据下拉选择设置验证?,jquery,asp.net-mvc,validation,Jquery,Asp.net Mvc,Validation,以下是我的查看代码: <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-co
<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>
@LabelFor(model=>model.device.HasAMC,htmlAttributes:new{@class=“controllabel col-lg-4”})
@EditorFor(model=>model.device.HasAMC,new{htmlAttributes=new{@class=“form control”})
@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/yyyyy)”,required=“required”,title=“Enter AMC Expire Date”})
保存
在这里,我需要设置必填字段“AMCExpiredDate”,就像“HasAMC”的值选择“true”一样,然后对必填字段应用验证,如果为false,则从“AMCExpiredDate”中删除验证
这在MVC中可能吗?
请帮帮我。提前谢谢
把你的观点改成这样
考虑一个<代码> [Reavdif] < /C>或类似的验证属性(或者您可以自己编写)可能的复制品,只需在jchange中执行,在Onchange事件上,将验证属性添加到DROPNDOWN感谢中。但我只需要在客户端而不是服务器端使用它,所以这并不是什么大问题,您所需要的只是在按钮函数“onclick=”javascript:Save(@ViewBag.DealerId)后面放置一个验证逻辑;“例如,代码if($('#HasAMC').val()==true){//验证日期}”
<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");
}
}