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开发者_C百科?h=yes
so i can do this
<% if request.querystring("h") = "yes" then %>
jquery stuff
<% else %>
don't do jquery stuff
<% end if %>
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" }
) %>
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
Are you sure you need to be doing it like that from the server.
You could instead follow an Unobtrusive Javascript/Progressive Enhancement approach.
精彩评论