How To Pass A Model Field To A Javascript Function In A View?
I have a simple question that has been vexing me tonight... I have an ASP.NET MVC4 + Razor application. When the user clicks a radio button, I want to pass a string value from a
Solution 1:
You shouldn't be doing
onClick = "TimezoneClicked(@model.Timezone)"
when you use that within the html helper function, it gets treated as a normal string. Instead you should be doing the following:
onClick = "TimezoneClicked('" + Model.Timezone + "')"
This will pass the Timzone value in the object to the function. If it's still empty, try to print it out like the following to make sure the property is not empty.
<span> @Model.Timezone </span>
Solution 2:
You can try the below code:
@Html.RadioButtonFor(model => model.TimezoneSource, "C",
new {propertyName = "UpdateTimezoneRadioButton",
onClick = "TimezoneClicked('" + Model.Timezone + "')" })UpdateTimezonefunctionTimezoneClicked(timezone) {
alert(timezone);
}
Solution 3:
If you need to get any of the dom elements in your function you could do this:
document.getElementById('@Html.IdFor(model => model.Draw)')
Post a Comment for "How To Pass A Model Field To A Javascript Function In A View?"