In one of my Views I alwas get a javascript error when I leave the focus of the Dropdownlist.
The error occurs in parseJSON in this line:
{return e.JSON&&e.JSON.parse?e.JSON.parse(n):null===n?n:"string"==typeof n&&(n=b.trim(n),n&&k.test(n.replace(S,"@").replace(A,"]").replace(E,"")))?Function("return "+n)():(b.error("Invalid JSON: "+n),t)}
The Model looks like this:
[Required]
[LocalizedDisplayName("Language")]
public int PreferredLanguageId { get; set; }
And the cshtml is here:
div class="type-text">
@(Html.Kendo().DropDownListFor(m => m.PreferredLanguageId)
.Name("PreferredLanguageId")
.SelectedIndex(Model.PreferredLanguageId)
.BindTo(LeoHelper.GetLanguages())
.DataTextField("Text")
.DataValueField("Value")
)
</div>
Although the javascript Exception is thrown, the value is correctly saved.
brgds
Malcolm Howlett
6 Answers, 1 is accepted
From the provided information it's not clear for us what is the exact reason for this behavior - could you please provide runable project where the issue is reproduced? This would help us pinpoint the exact reason for this behavior.
Kind Regards,
Vladimir Iliev
the Telerik team
Here's the JSON object I caught with firebug: [{"salesgeoid":5,"shortname":"ALL"},{"salesgeoid":1,"shortname":"BRA"},{"salesgeoid":3,"shortname":"CHE"},{"salesgeoid":2,"shortname":"FRA"}]
Here's my view:
<
div
class
=
"editor-field"
>@(Html.Kendo().DropDownListFor(model => model.salesgeoid)
.Name("salesgeo")
.HtmlAttributes(new { style = "width:150px" })
.DataTextField("shortname")
.DataValueField("salesgeoid")
.DataSource(source =>
{
source.Read(read =>
{
read.Action("SalesGeoList", "Profile");
});
}))</
div
>
And my controller:
public
JsonResult SalesGeoList()
{
return
Json(db.SalesGeo.Select(x =>
new
{ salesgeoid = x.salesgeoid, shortname = x.shortname }), JsonRequestBehavior.AllowGet);
}
I reviewed the provided information, however currently I'm unable to reproduce the issue on our side. I would suggest to try isolating the problem in a sample project and send it back to us. This would help us pinpoint the exact reason for this behavior.
Also I would suggest to remove the Name configuration option as it overrides the name generated from the model property that is bound to the DropDownList.
Vladimir Iliev
Telerik
However I followed your advice and I removed the name attribute, that helped me to access the data in the controller (it wasn't possible before). But the error message still appears in firebug. The message is : "SyntaxError: JSON.parse: unexpected character" and the file is "jquery.min.js (line 3)". Maybe this last information may help you find the source of this problem ?
Kind regards
Basically the data sent by the controller and the DropDownList configuration looks valid - most probably the issue is related to the scripts included in the project. I would suggest to check for duplicate script includes and make sure that all scripts are the correct versions (for example jQuery should be v1.9.1). For more information about the required scripts I would suggest to check this article.
Vladimir Iliev
Telerik