验证不适用于 kendoUI 多选
本文关键字:多选 kendoUI 适用于 不适用 验证 | 更新日期: 2023-09-27 18:31:02
我正在使用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
验证。
您需要验证值而不是列表对象。
您可能需要在模型中进行以下更改:
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
属性是否具有任何值。
如果它没有任何内容,则验证应该有效。
希望对您有所帮助。