我有一个下拉列表,其值为Yes, No
。如果您选择yes
我已验证下两个文本框。如何根据下拉选择进行依赖性验证?
public class MeetingAbstract
{
public string HasMaterialPublishedElseWhereLabel { get; set; }
public string HasMaterialPublishedElseWhereOptions { get; set; }
[Required]
public string HasMaterialPublishedElseWhereText { get; set; }
public string DtPublishedTimeId { get; set; }
public string DtPublishedTimeLabel { get; set; }
//validate this based on HasMaterialPublishedElseWhereText =Yes value
public string DtPublishedTimeText { get; set; }
public string PublishedPlaceId { get; set; }
public string PublishedPlaceLabel { get; set; }
//validate this based on HasMaterialPublishedElseWhereText =Yes value
public string PublishedPlaceText { get; set; }
}
<div class="row" style="padding-bottom: 10px">
<div class="col-md-10">
<div class="col-md-6">@Html.Label(Model.HasMaterialPublishedElseWhereLabel,
new {@class = "control-label mandatory"})</div>
<div class="col-md-4">
@{
options = Model.HasMaterialPublishedElseWhereOptions;
optionsList = options.Split(',').ToList();
optionSelect = optionsList.Select(option => new SelectListItem()
{Text = option, Value = option}).ToList();
}
@Html.DropDownListFor(model => model.HasMaterialPublishedElseWhereText,
optionSelect, i18n_Models_Abstract.SelectOption, new { @class = "input-
validation-error form-control" })
@Html.ValidationMessageFor(model => model.HasMaterialPublishedElseWhereText,
i18n_Models_Abstract.RequiredField,
new { style = "padding-left: 5px" })
</div>
</div>
</div>
<div class="row" style="padding-bottom: 10px">
<div class="col-md-10">
<div class="col-md-6">@Html.Label(Model.DtPublishedTimeLabel, new {@class =
"control-label mandatory"})</div>
<div class="col-md-4">@Html.TextBoxFor(model => model.DtPublishedTimeText, new \
{@class = "form-control", @placeholder = Model.DtPublishedTimeLabel,
required = "required", maxlength = 40})
@Html.ValidationMessageFor(model => model.DtPublishedTimeText,
i18n_Models_Abstract.RequiredField, new { style = "padding-left: 5px" })</div>
</div>
</div>
<div class="row" style="padding-bottom: 10px">
<div class="col-md-10">
<div class="col-md-6">@Html.Label(Model.PublishedPlaceLabel, new {@class =
"control-label mandatory"})</div>
<div class="col-md-4">@Html.TextBoxFor(model => model.PublishedPlaceText, new
{@class = "form-control", @placeholder = Model.PublishedPlaceLabel,
required = "required", maxlength = 40})
@Html.ValidationMessageFor(model => model.PublishedPlaceText,
i18n_Models_Abstract.RequiredField, new { style = "padding-left: 5px" })
</div>
</div>
</div>
答案 0 :(得分:1)
我会在您的模型上添加Validate
方法,该方法将通过ModelState.IsValid
调用并公开:
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) {
if (this.HasMaterialPublishedElseWhereText == "Yes") {
yield return new ValidationResult("This isn't valid! Let me tell you why...");
}
}
然后在您的控制器中,只需致电:
if (ModelState.IsValid) {
//You can only get this far if your Validate method
//doesn't return any ValidationResult objects.
//Do your magic!
}
显然,这不会在客户端执行,因此如果客户端上的验证是必需的,那么您可能需要将其转入。
最后一点需要注意的是,如果您只有两个值(例如“是”和“否”),则单选按钮可能比下拉更有意义。