最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asp.net mvc - How to pass a model field to a Javascript function in a view? - Stack Overflow

programmeradmin1浏览0评论

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
Add a ment  | 

3 Answers 3

Reset to default 4

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>

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)')

发布评论

评论列表(0)

  1. 暂无评论