Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating an action URL in JavaScript for ASP.NET MVC

I'm trying to redirect to another page by calling an action in controller with a specific parameter. I'm trying to use this line:

window.open('<%= Url.Action("Report", "Survey",
    new { id = ' + selectedRow + ' } ) %>');

But I couldn't make it work; it gives the following error:

CS1012: Too many characters in character literal.

Can't I generate the action URL this was on the client side? Or do I have to make an Ajax call by supplying the parameter and get back the needed URL? This doesn't seem right, but I want to if it's the only way.

Is there an easier solution?

like image 810
canxss Avatar asked Jan 06 '10 11:01

canxss


People also ask

What is a URL action?

A URL action is a hyperlink that points to a web page, file, or other web-based resource outside of Tableau. You can use URL actions to create an email or link to additional information about your data. To customize links based on your data, you can automatically enter field values as parameters in URLs.

How do I set an area in URL action?

You can use this Url. Action("actionName", "controllerName", new { Area = "areaName" }); Also don't forget to add the namespace of the controller to avoid a conflict between the admin area controller names and the site controller names.

What is action ASP NET MVC?

Advertisements. ASP.NET MVC Action Methods are responsible to execute requests and generate responses to it. By default, it generates a response in the form of ActionResult. Actions typically have a one-to-one mapping with user interactions.

What is difference between HTML ActionLink and URL action?

Yes, there is a difference. Html. ActionLink generates an <a href=".."></a> tag whereas Url. Action returns only an url.


2 Answers

Remember that everything between <% and %> is interpreted as C# code, so what you're actually doing is trying to evaluate the following line of C#:

Url.Action("Report", "Survey", new { id = ' + selectedRow + ' } )

C# thinks the single-quotes are surrounding a character literal, hence the error message you're getting (character literals can only contain a single character in C#)

Perhaps you could generate the URL once in your page script - somewhere in your page HEAD, do this:

var actionUrl =
    '<%= Url.Action("Report", "Survey", new { id = "PLACEHOLDER" } ) %>';

That'll give you a Javascript string containing the URL you need, but with PLACEHOLDER instead of the number. Then set up your click handlers as:

window.open(actionUrl.replace('PLACEHOLDER', selectedRow));

i.e. when the handler runs, you find the PLACEHOLDER value in your pre-calculated URL, and replace it with the selected row.

like image 100
Dylan Beattie Avatar answered Sep 16 '22 16:09

Dylan Beattie


I usually declare a javascript variable in the section to hold the root of my website.

<%="<script type=\"text/javascript\">var rootPath = '"
    + Url.Content("~/") + "';</script>" %>

To solve your problem I would simply do

window.open(rootPath + "report/survey/" + selectedRow);
like image 42
Jaco Pretorius Avatar answered Sep 19 '22 16:09

Jaco Pretorius