【问题标题】:Validation not worked on kendoUI multiselect验证不适用于剑道 UI 多选
【发布时间】:2014-11-19 11:09:13
【问题描述】:

我正在使用 MVC4 创建一个表单。 这里我使用了剑道多选并尝试通过模型注释进行验证。

我的代码是:

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

当我点击提交按钮时,验证错误消息同时出现在“名称”和“别名”字段上,但不在“供应商”字段上。

我不想使用javascript 验证。

【问题讨论】:

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


【解决方案1】:

您需要验证 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 属性是否具有任何值。

如果它没有任何内容,则验证应该起作用。

希望对你有帮助。

【讨论】:

  • 不,它仍然无法正常工作。通过检查元素,我发现 kendo 隐藏了
  • 看起来您没有传递int(数字)值。将您的模型属性设为字符串并尝试。 例如: public string VendorId { get; set; }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多