在MVC应用程序中缺少表单验证

时间:2016-05-25 07:42:46

标签: c# asp.net-mvc validation

我有一个只包含复选框的表单,我知道我可以使每个表单都强制执行验证错误。但是,如果没有选中任何一个方框,我正在寻找的是一个错误。我将如何实现这一目标?

我正在寻找错误消息,例如:"您必须至少选择一个属性。"

我应该澄清,没有一个字段是单独需要的,应该至少有一个选择的选项。

编辑以澄清:

我的观点看起来像这样:

@using (Html.BeginForm("Method", "Controller", FormMethod.Post, new {id = "id"}))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })

   <div class="form-group">
       @Html.LabelFor(model => model.Property1, htmlAttributes: new { @class = "control-label col-md-4" })
       <div class="col-md-8">
          @Html.CheckBoxFor(model => model.Property1)
       </div>
    </div>
    <div class="form-group">
       @Html.LabelFor(model => model.Property2, htmlAttributes: new { @class = "control-label col-md-4" })
        <div class="col-md-8">
            @Html.CheckBoxFor(model => model.Property2)
        </div>
     </div>
     <div class="form-group">
        @Html.LabelFor(model => model.Property3, htmlAttributes: new { @class = "control-label col-md-4" })
        <div class="col-md-8">
           @Html.CheckBoxFor(model => model.Property3)
        </div>
     </div>
}

My View模型如下所示:

public class FormVM 
{
   [Display(Name = "One")]
   public bool Property1 {get;set;}
   [Display(Name = "Two")]
   public bool Property2 {get;set;}
   [Display(Name = "Three")]
   public bool Property3 {get;set;}
}

3 个答案:

答案 0 :(得分:1)

如果仅在一个地方使用此验证,则可以在Action方法中验证它:

searchArray = [10,20,30,40,50,60,100,80,90,110]
lower_bound = 3  # the lower bound is inclusive, i.e. element 3 is the first checked one
upper_bound = 9  # the upper bound is exclusive, i.e. element 8 (9-1) is the last checked one

max_index, max_value = max(enumerate(searchArray[lower_bound:upper_bound], lower_bound),
                           key=lambda x: x[1])

print max_index, max_value
# output: 6 100

如果您可能会更多地重复使用此验证,我建议您编写验证属性。

答案 1 :(得分:1)

由于问题没有提及最好使用哪种服务器端或客户端验证,因此有两种方法。

1)服务器端验证(没有ModelState.AddModelError):

Controller.cs

[HttpPost]
public ActionResult Method(FormVM model)
{
    if (ModelState.IsValid) 
    {
        // ... other processing code and action returns
    }

    if (!(model.Property1 || model.Property2 || model.Property3))
    {
        ViewData["Error"] = "You must select at least one property." // this can be changed with ViewBag

        // immediately return the same page
        return View(model);
    }
}

View.cshtml:

<p>@ViewData["Error"]</p>

2)使用vanilla JS进行客户端验证,使用id识别所需元素:

<script type="text/javascript">
var check1 = document.getElementById("check1").value;
var check2 = document.getElementById("check2").value;
var check3 = document.getElementById("check3").value;

if (!(check1 || check2 || check3))
{
     document.getElementById("error").innerHTML = "You must select at least one property.";
}
else
{
     document.getElementById("error").innerHTML = '';
}
</script>

<div class="form-group">
       @Html.LabelFor(model => model.Property1, htmlAttributes: new { @class = "control-label col-md-4" })
       <div class="col-md-8">
          @Html.CheckBoxFor(model => model.Property1, htmlAttributes: new { @id = "check1" })
       </div>
    </div>
    <div class="form-group">
       @Html.LabelFor(model => model.Property2, htmlAttributes: new { @class = "control-label col-md-4" })
        <div class="col-md-8">
            @Html.CheckBoxFor(model => model.Property2, htmlAttributes: new { @id = "check2" })
        </div>
     </div>
     <div class="form-group">
        @Html.LabelFor(model => model.Property3, htmlAttributes: new { @class = "control-label col-md-4" })
        <div class="col-md-8">
           @Html.CheckBoxFor(model => model.Property3, htmlAttributes: new { @id = "check3" })
        </div>
     </div>
</div>
<div id="error"></div>

答案 2 :(得分:1)

您可以在viewmodel上实现IValidatableObject界面:

public class FormVM : IValidatableObject
{
    [Display(Name = "One")]
    public bool Property1 {get;set;}

    [Display(Name = "Two")]
    public bool Property2 {get;set;}

    [Display(Name = "Three")]
    public bool Property3 {get;set;}

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        var results = new List<ValidationResult>();

        if (!(Property1 || Property2 || Property3))
        {
            results.Add(new ValidationResult("You must select at least one property."));
        }

        return results;
    }
}

使用此功能的好处是,如果您在控制器中调用ModelState.IsValid,并且错误消息已添加到ModelState错误,则会自动触发此操作。