如何从Range属性获取Minimum / Maximum值

时间:2013-09-25 14:16:11

标签: asp.net-mvc-4

我添加的视图如下所示,我使用了模型的范围属性。

@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;
    }
}

如何在此处获取最小值和最大值?我没有在控制器下为每种类型的验证创建一个单独的方法。

1 个答案:

答案 0 :(得分:0)

您的输入需要包含在以下格式中:

@using (Html.BeginForm())
{
    ...
}

并且您的控制器操作应该接收View Model作为参数:

public ActionResult AddValidator(RangeAttribute rangeAttribute)
{
    rangeAttribute.Maximum;
    rangeAttribute.Minimum;
}