admin管理员组文章数量:1356702
I am using C#, MVC3, and Razor.
I have a javascript function (in the view) that gets called when a particular menu item is clicked. In this function, I need to build a new URL with parameters (based on other selections on the screen) and redirect to it. It want it to do something like this:
ValueA and ValueB are variables in the javascript section and are populated with values.
function doSomething(ID) {
location.href = "../Area/Controller/Action?ID=" + ID + "&ValueA=" + ValueA + "&ValueB=" + ValueB;
}
However, due to the nature of MVC I need to make sure the URL is always right, regardless of how the user got to the page. I've tried to use @Url.Content("") (see next code block) but the issue I run into is:
- The name 'ID' does not exist in the current context
- The name 'ValueA' does not exist in the current context
- The name 'ValueB' does not exist in the current context
Here is an example of what I would like to do but get the above mentioned errors on:
function doSomething(ID) {
location.href = @Url.Content("~/Area/Controller/Action?ID=" + ID + "&ValueA=" + ValueA + "&ValueB=" + ValueB);
}
How can I make this work? Is there a better way?
Thanks, Tony
I am using C#, MVC3, and Razor.
I have a javascript function (in the view) that gets called when a particular menu item is clicked. In this function, I need to build a new URL with parameters (based on other selections on the screen) and redirect to it. It want it to do something like this:
ValueA and ValueB are variables in the javascript section and are populated with values.
function doSomething(ID) {
location.href = "../Area/Controller/Action?ID=" + ID + "&ValueA=" + ValueA + "&ValueB=" + ValueB;
}
However, due to the nature of MVC I need to make sure the URL is always right, regardless of how the user got to the page. I've tried to use @Url.Content("") (see next code block) but the issue I run into is:
- The name 'ID' does not exist in the current context
- The name 'ValueA' does not exist in the current context
- The name 'ValueB' does not exist in the current context
Here is an example of what I would like to do but get the above mentioned errors on:
function doSomething(ID) {
location.href = @Url.Content("~/Area/Controller/Action?ID=" + ID + "&ValueA=" + ValueA + "&ValueB=" + ValueB);
}
How can I make this work? Is there a better way?
Thanks, Tony
Share Improve this question asked Sep 8, 2011 at 18:08 Anthony QueenAnthony Queen 2,3683 gold badges18 silver badges21 bronze badges1 Answer
Reset to default 6You should concatenate the the static part to the dynamic part:
location = "@Url.Content("~/Area/Controller/Action")?ID=" + ID + "&ValueA=" + ValueA + "&ValueB=" + ValueB;
The outer "@...?ID="
is a Javascript string literal.
@Url.Content("...")
is server-side code that emits raw text into the Javascript literal.
本文标签: aspnet mvc 3MVC3Dynamic URL redirect in JavascriptStack Overflow
版权声明:本文标题:asp.net mvc 3 - MVC3 - Dynamic URL redirect in Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744006613a2574817.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论