简体   繁体   中英

Add URL.Action to javascript method in Razor View

I have a question on how to put my Url Action to razor view:

@Html.EditorFor(m => m.MyTypes, false, new {id = "myId", onchange = "onMyTypeChange('Url.Action("GetMyFields", "MyController")')"})

Do you have any idea how to fix it ?

There seems to be a couple of problems in your syntax for this line of code.

@Html.EditorFor(m => m.MyTypes, false, new {id = "myId", onchange = "onMyTypeChange('Url.Action("GetMyFields", "MyController")')"})

Your second parameter false is useless, since that technically should be of type string since it is for a templateName based on this .

Also as Chris Pratt and I were discussing, in MVC 5.1+ you have to pass your HTML attributes with new { htmlAttributes = new {...} } .

This should help in solving your issue.

@Html.EditorFor(m => m.MyTypes, new { htmlAttributes = new { id = "myId", onchange = "onMyTypeChange('" + Url.Action("GetMyFields", "MyController") + "')" } })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM