我添加的视图如下所示,并且我使用了模型的范围属性。
@Model System.ComponentModel.DataAnnotations.RangeAttribute
@{Layout = null;}
@Html.Hidden("IncludedValidators", "RangeAttribute")
<div class="page-header">
<h4>
MinMaxRangeValidator
</h4>
<div>
<a href="javascript:RemoveValidation(MinMaxRangeValidator);"><span class="icon-remove pull-right"></span></a>
</div>
</div>
<div class="control-group">
@Html.LabelFor(p => p.Minimum, new { @class = "control-label" })
<div class="controls">
@Html.TextBoxFor(p => p.Minimum, new { @class = "input-medium" })
</div>
</div>
<div class="control-group">
@Html.LabelFor(p => p.Maximum, new { @class = "control-label" })
<div class="controls">
@Html.TextBoxFor(p => p.Maximum, new { @class = "input-medium" })
</div>
</div>
<div class="control-group">
@Html.LabelFor(p => p.ErrorMessage, new { @class = "control-label" })
<div class="controls">
@Html.TextBoxFor(p => p.ErrorMessage, new { @class = "input-medium" })
</div>
</div>
我的控制器看起来像:
public ActionResult AddValidator(string validatorType)
{
ValidationAttribute validator = null;
switch (validatorType)
{
case "RequiredAttribute":
validator = new RequiredAttribute();
break;
case "RangeAttribute":
validator = new RangeAttribute(minimum,maximum);
break;
}
}
我如何在这里获得最小值和最大值?我没有在控制器下为每种类型的验证创建单独的方法。