mskuratowski
mskuratowski

Reputation: 4124

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 ?

Upvotes: 0

Views: 243

Answers (1)

Grizzly
Grizzly

Reputation: 5943

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") + "')" } })

Upvotes: 1

Related Questions