Validation didn't work on kendoUI multi-element
I am creating a form using MVC4. I have used kendo multiselect here and will try to validate via model annotation.
My code:
@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>
My model:
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; }
}
When I clicked the Submit button, the validation error appears in both the Name and Alias ββfields, but not in the Suppliers field.
I don't want to use validation javascript
.
+3
source to share
1 answer
You need to check the value, not the list object.
You may need to make the following changes to your model:
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; }
}
In your view:
<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>
Make sure the property VendorId
contains any value or not when submitting the form .
If it has nothing, the validation should work.
Hope it helps.
0
source to share