0

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 ?

11
  • where is the leading '? you have an ending ' after "MyController")' but where is the leading one? Commented Jun 17, 2016 at 12:48
  • I fixed it, bu it's still now working Commented Jun 17, 2016 at 12:49
  • try '@Url.Action Commented Jun 17, 2016 at 12:49
  • @BviLLe_Kid: Not true. Only reserved keywords need to be prefixed with @, such as @class, because class is a keyword and will be misinterpreted otherwise. id is not a keyword, and is fine. Commented Jun 17, 2016 at 13:00
  • @ChrisPratt ahh okay, I didn't know that. But his onchange needs to be prefixed with @, right? Commented Jun 17, 2016 at 13:02

1 Answer 1

1

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") + "')" } })
Sign up to request clarification or add additional context in comments.

2 Comments

@ChrisPratt Thanks for the edit haha it is early, I don't why I had that line in there about prefixing the @ haha completely forgot about the keyword discussion.. off to dunkin donuts I go
No problem. It took me 4 edits to get my edit right ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.