我正在做一些自定义验证,当选择了保管箱选定的值并且根据该值应该选择其他属性时,它会起作用。我想知道是否可以根据选择的下拉值为控件创建自定义消息。我目前有一部分工作,但是我只能有一个消息用于1个控制器。我想知道我是否在每个级别都进入了IF语句,是否可以相应地显示错误消息。这是我的代码,我希望听到并看到建议的任何内容以及应该做或应该做得更好的事情。谢谢。
//part of model [Required]
[CheckDropDown]
public string Usage { get; set; }
/* INT OR STRING ?? */
/* If USAGE selection no imm reqs pass else REQPROJ required */
[Display(Name = "Required Project(s)")]
public string RequiredProject { get; set; }
// [Required]
[Range(0, Int32.MaxValue)]
//[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:#}")]
public int Quantity { get; set; }
//if quantity picked than units required
// [Required]
public string Units { get; set; }
public class CustomValidation
{
public sealed class CheckDropDown : ValidationAttribute
{
public string DropDownValue { get; set; }
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
//return base.IsValid(value, validationContext);
//var model = (CustomVaildModel)validationContext.ObjectInstance;
var model = (PartNumberViewModel)validationContext.ObjectInstance;
//if(model.Usage == "1" && model.RequiredProject != null)
if(model.Usage == "1")
{
if(model.RequiredProject != null)
{
var errorMsg = "If there are 'No Immediate Requirements' for this part number, then a 'Required Project' is not necessary.";
return new ValidationResult(errorMsg);
}
else
{
return ValidationResult.Success;
}
}
else if(model.Usage == "2" && model.Quantity.Equals(0) && model.Units == null)
{
//FormatErrorMessage(validationContext.DisplayName);
var errorMsg = "When Usage = 'Procure for Projects listed' Quantity and Units CANNOT BE EMPTY!";
return new ValidationResult(errorMsg);
}
else
{
return ValidationResult.Success;
}
}
}//view part <div class="form-group row">
<div class="col-md-4 col-lg-4">
@Html.LabelFor(x => x.Usage,
htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-md-8 col-lg-8">
@Html.DropDownListFor(x => x.Usage,
(IEnumerable<SelectListItem>) ViewBag.UsageDDL,
"", new { @class = "form-control" })
@Html.ValidationMessageFor(x => x.Usage, "",
new { @class = "text-danger" })
</div>
</div>
<div class="form-group row">
<div class="col-md-4 col-lg-4">
@Html.LabelFor(x => x.RequiredProject,
htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-md-8 col-lg-8">
@Html.EditorFor(x => x.RequiredProject, new { htmlAttributes =
new { @class = "form-control" }})
@Html.ValidationMessageFor(x => x.RequiredProject, "",
new { @class = "text-danger" })
</div>
</div>
@* Think about Textboxes on SAME line or SEPERATE *@
<div class="form-group row">
<div class="col-md-4 col-lg-4">
@Html.LabelFor(x => x.Quantity,
htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-md-8 col-lg-8">
@Html.EditorFor(x => x.Quantity, new { htmlAttributes =
new { @class = "form-control", @type = "number" }})
@Html.ValidationMessageFor(x => x.Quantity, "",
new { @class = "text-danger" })
</div>
</div>
<div class="form-group row">
<div class="col-md-4 col-lg-4">
@Html.LabelFor(x => x.Units,
htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-md-8 col-lg-8">
@Html.DropDownListFor(x => x.Units,
(IEnumerable<SelectListItem>) ViewBag.UnitsDDL,
"", new { @class="form-control" })
@Html.ValidationMessageFor(x => x.Units, "",
new { @class = "text-danger" })
</div>
</div>