Roberto
Roberto

Reputation: 11

Need to pass a querystring variable in MVC, so i can use it with jquery?

I would like to create this url blah.com/preview?h=yes

so i can do this

<% if request.querystring("h") = "yes" then %>
jquery stuff
<% else %>
don't do jquery stuff
<% end if %>

Upvotes: 1

Views: 1182

Answers (3)

eglasius
eglasius

Reputation: 36037

Are you sure you need to be doing it like that from the server.

You could instead follow an Unobtrusive Javascript/Progressive Enhancement approach.

Upvotes: 0

ten5peed
ten5peed

Reputation: 15890

Set a property on your view model that you can inspect.

E.g. ViewModel

public class SomeActionViewModel
{
    public bool DoJquery { get; set; }
}

Action (called via http://www.myawesomesite.com/somecontroller/someaction?h=yes)

public ActionResult SomeAction(string h)
{
    var viewModel = new SomeActionViewModel();

    if (!string.IsNullOrWhiteSpace(h) && (h.ToLower() == "yes"))
        viewModel.DoJquery = true;

    return View(viewModel);
}

View

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<SomeActionViewModel>" %>

<% if (ViewModel.DoJquery) { %>
    <!-- Do jQuery -->
<% } else { %>
    <!-- Don't do jQuery -->
<% } %>

HTHs,
Charles

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038770

You could use an HTML helper:

<%= Html.ActionLink(
    "some text", 
    "someaction", 
    "somecontroller", 
    new { h = "yes" },
    null
) %>

Assuming default routes this will generate the following link:

<a href="/somecontroller/someaction?h=yes">some text</a>

Or if you want to generate only the link you could use the Url helper:

<%= Url.Action(
    "someaction", 
    "somecontroller", 
    new { h = "yes" }
) %>

Upvotes: 1

Related Questions