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

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

4

1 回答 1

0

您的输入需要以如下形式包装:

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

并且您的控制器操作应该接收视图模型作为参数:

public ActionResult AddValidator(RangeAttribute rangeAttribute)
{
    rangeAttribute.Maximum;
    rangeAttribute.Minimum;
}
于 2013-09-25T14:32:41.253 回答