验证不适用于kendo UI multiselect

时间:2014-11-19 11:09:13

标签: c# asp.net-mvc asp.net-mvc-4 kendo-ui data-annotations

我正在使用MVC4创建表单。 在这里,我使用了kendo multiselect并尝试通过模型注释进行验证。

我的代码是:

@model WEBAPP._2012.Models.DeviceInventory.DeviceTechnologyModel

<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

@using (Ajax.BeginForm("Create", "Device_TechnologyInventory", new AjaxOptions { HttpMethod = "POST", OnSuccess = "onSuccessAddTechnology" })) { @Html.ValidationSummary(true)

<table width="100%" cellspacing="0" cellpadding="0" border="0">
  <tr>
    <td>
      <@Html.LabelFor(model=>model.Name)</td>
    <td class="editor-field" width="160px;">
      @Html.EditorFor(model => model.Name) @Html.ValidationMessageFor(model => model.Name)
    </td>
  </tr>

  <tr>
    <td>@Html.LabelFor(model => model.Alias)</td>
    <td class="editor-field">
      @Html.EditorFor(model => model.Alias) @Html.ValidationMessageFor(model => model.Alias)
    </td>
  </tr>

  <tr>
    <td>@Html.LabelFor(model => model.Vendors)/td>
      <td class="editor-field">
        @(Html.Kendo().MultiSelectFor(model=>model.Vendors) .Name("Vendors").Placeholder("Select") .BindTo(new SelectList(ViewBag.VendorList, "Value", "Text")) ) @Html.ValidationMessageFor(model => model.Vendors)
      </td>
  </tr>
</table>

<div class="CreateWrp">
  <input type="submit" value="Create " class="btn-success"/>
  <input type="reset" value="Reset " class="btn-primary" />
</div>

我的模特是:

    public class DeviceTechnologyModel
    {
        public int Id { get; set; }
        [Required(ErrorMessage = "*")]
        [Display(Name = "Device Technology")]
        public string Name { get; set; }
        [Required(ErrorMessage = "*")]
        [Display(Name = "Alias")]
        public string Alias { get; set; }
        [Required(ErrorMessage = "*")]
        [Display(Name = "Device vendors")]
        public List<string> Vendors { get; set; }
    }

当我点击提交按钮时,验证错误消息会出现在&#34;名称&#34;和#34; Alias&#34;字段但不在字段上#34;供应商&#34;。

我不想使用javascript验证。

1 个答案:

答案 0 :(得分:0)

您需要验证Value而不是列表对象。

您可能需要在模型中进行以下更改:

public class DeviceTechnologyModel
{
        public int Id { get; set; }
        [Required(ErrorMessage = "*")]
        [Display(Name = "Device Technology")]
        public string Name { get; set; }
        [Required(ErrorMessage = "*")]
        [Display(Name = "Alias")]
        public string Alias { get; set; }

        [Required]
        [Required(ErrorMessage = "*")]
        [Display(Name = "Device vendors")]
        public int VendorId { get; set; }

        public List<string> Vendors { get; set; }
}

在您的视图中:

<tr>
    <td>@Html.LabelFor(model => model.Vendors)/td>
      <td class="editor-field">
        @(Html.Kendo().MultiSelectFor(model=> model.VendorId)
                      .Name("Vendors").Placeholder("Select")
                      .BindTo(new SelectList(ViewBag.VendorList, "Value", "Text"))) 
       @Html.ValidationMessageFor(model => model.VendorId)
      </td>
  </tr>

确保在提交表单时,VendorId属性是否包含任何值。

如果没有任何内容,则验证应该有效。

希望它有所帮助。