0
我添加的視圖如下所示,並且我使用了模型的範圍屬性。如何從「範圍」屬性中獲取最小/最大值
@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;
}
}
如何獲得的最小值和最大值這裏價值觀?我沒有在控制器下爲每種驗證類型創建一個單獨的方法。
但是看看控制器I我正在使用一個開關盒。我應該爲每個項目創建一個單獨的控制器方法。 –
@JitheshChandra那麼這是一個不同的問題,更重要的是一個意見。以我建議的方式使用模型綁定將需要不同的操作,因爲它們需要不同的參數。 – asymptoticFault