简体   繁体   English

在Razor View中将URL.Action添加到javascript方法

[英]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 . 您的第二个参数false是无用的,因为从技术上讲,该参数应为string类型,因为它用于基于thistemplateName

Also as Chris Pratt and I were discussing, in MVC 5.1+ you have to pass your HTML attributes with new { htmlAttributes = new {...} } . 同样,正如Chris Pratt和我所讨论的那样,在MVC 5.1+中,您必须使用new { htmlAttributes = new {...} }传递HTML属性。

This should help in solving your issue. 这应该有助于解决您的问题。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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