MinSIL MinSIL - 5 months ago 61
jQuery Question

Kendo MultiSelect Cannot read property 'value'

It is my PartialView:

.......

<div class="form-group">
@Html.LabelFor(model => model.securities, htmlAttributes: new { @class = "control-label col-md-2"})
<div class="col-md-10">

@(Html.Kendo().MultiSelect()
.Name("productMultiSelect")
.DataTextField("label")
.DataValueField("value")Product to be used by the multiselect as a value.
.HtmlAttributes(new { style = "width:350px; height:350px", @id = "prd" })
.Filter(FilterType.Contains)
.DataSource(source =>
{
source.Read(read =>
{
read.Action("GetLogin", "IT_Inventory");
})
.ServerFiltering(false);
})
.Value(ViewBag.SelectedItem2)
.ItemTemplate("<span><h3 style=\"font-size: 1.2em;font-weight: normal;margin: 0 0 1px 0;padding: 0;\">#: data.label #</h3><p style=\"margin:0;padding:0;font-size: .8em; \">#: data.desc #</p></span>")
)
</div>
</div>


My button:

<button type="button" id="btnSave" class="btn btn-success btn-lg">Save </button>


My JS:

<script>
$("#btnSave").click(function (e) {
e.preventdefault();
$.ajax({
type: 'POST',
url: '@Url.Action("SignIT", "IT_Inventory")',
data: JSON.stringify({ productMultiSelect: $("#productMultiSelect").data("kendoMultiSelect").value(), id: $("#id").val(), SomeBooleanProperty: false }),
dataType: 'json',
contentType: 'application/json',
success: function (data) {
if (data == true) {
$("#onInsert").data("kendoWindow").close();
}
else {
alert("Error!");
}
},
error: function () {
alert("An error has occured!!!");
}
});
});
</script>


When I try post to controller I am getting
Uncaught TypeError: Cannot read property 'value' of undefined
. For id and SomeBooleanProperty is ok.
When I use submit form is ok.
I should post data to controller list of productMultiSelect?

Answer

Looks like you havn't initialize you kendoMultiSelect, or it's in another div;

Check to see it:

console.log($("#productMultiSelect").length); //should be at least 1, if 0, then you initialized kendo in come other div

and

console.log($("#productMultiSelect").data("kendoMultiSelect")); //should be an object - if undefined, then you have not initialized kendo