admin管理员组文章数量:1333419
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 field in the model, "Timezone", to the onclick handler, which is a javascript function. In the javascript function, I will take the string and do some work, but for now, I simply pop-up an alert(). I have found a lot of references to having to do special quoting, and I've tried all of them, but no matter what I do, the alert shows nothing, but the form that has the radio button shows me a valid value in its Timezone textbox.
I would really appreciate your help to figure out what I am doing wrong... If this is not possible (why?), then can I access the model's value from the javascript function? I tried @model.Timezone, but always have an empty string. Really vexing stuff when I know that it must be something really easy! Anyway, here is the code for the radio button and the simple javascript function. I'd really appreciate your help!
Thanks, Mike
@Html.RadioButtonFor(model => model.TimezoneSource, "C",
new {propertyName = "UpdateTimezoneRadioButton",
onClick = "TimezoneClicked(@model.Timezone)" })Update Timezone
function TimezoneClicked(timezone) {
alert(timezone);
}
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 field in the model, "Timezone", to the onclick handler, which is a javascript function. In the javascript function, I will take the string and do some work, but for now, I simply pop-up an alert(). I have found a lot of references to having to do special quoting, and I've tried all of them, but no matter what I do, the alert shows nothing, but the form that has the radio button shows me a valid value in its Timezone textbox.
I would really appreciate your help to figure out what I am doing wrong... If this is not possible (why?), then can I access the model's value from the javascript function? I tried @model.Timezone, but always have an empty string. Really vexing stuff when I know that it must be something really easy! Anyway, here is the code for the radio button and the simple javascript function. I'd really appreciate your help!
Thanks, Mike
@Html.RadioButtonFor(model => model.TimezoneSource, "C",
new {propertyName = "UpdateTimezoneRadioButton",
onClick = "TimezoneClicked(@model.Timezone)" })Update Timezone
function TimezoneClicked(timezone) {
alert(timezone);
}
Share
Improve this question
asked Dec 3, 2012 at 4:23
A Bit of HelpA Bit of Help
1,8063 gold badges24 silver badges40 bronze badges
1
- have you checked with the below code? – Rusty Commented Dec 4, 2012 at 6:52
3 Answers
Reset to default 4You 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>
You can try the below code:
@Html.RadioButtonFor(model => model.TimezoneSource, "C",
new {propertyName = "UpdateTimezoneRadioButton",
onClick = "TimezoneClicked('" + Model.Timezone + "')" })Update Timezone
function TimezoneClicked(timezone) {
alert(timezone);
}
If you need to get any of the dom elements in your function you could do this:
document.getElementById('@Html.IdFor(model => model.Draw)')
本文标签: aspnet mvcHow to pass a model field to a Javascript function in a viewStack Overflow
版权声明:本文标题:asp.net mvc - How to pass a model field to a Javascript function in a view? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742331411a2454768.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论