ASP.NET MVC Runtime Javascript Syntax Error without error code -
there javascript syntax error
form element:
@html.dropdownlistfor(model => model.unit, new selectlist(viewbag.unitlist, "value", "text"), "-- select unit --", new {onchange = "showselectedvalue" })
javascript:
function showselectedvalue() { alert('selected: ' + @model.unit); // syntax error underlines closing parentethesis of alert function }
i want check unit
being selected function showselectedvalue
,
razor executes server side , javascript executes client side can't mix them together; code put initial value of @model.unit
script string literal , alert('selected: ' + whatever unit is);
, syntax error because whatever unit is
not encased in quotes.
the easiest thing pass select
parameter onchange
function.
@html.dropdownlistfor(model => model.unit, new selectlist(viewbag.unitlist, "value", "text"), "-- select unit --", new {onchange = "showselectedvalue(this)" })
.
function showselectedvalue(ddlist) { alert('selected: ' + ddlist.value); }
Comments
Post a Comment