views:

55

answers:

3

SO 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 %>

A: 

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" }
) %>
Darin Dimitrov
A: 

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

Charlino
A: 

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

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

eglasius