I want to check the URL parameter in my Razor markup. For example, how do I do something like this:
<div id="wrap" class="@{if (URL "IFRAME" PARAMETER EQUALS 1) iframe-page}">
This question is related to
c#
asp.net
asp.net-mvc
razor
asp.net-mvc-4
Noneof the answers worked for me, I was getting "'HttpRequestBase' does not contain a definition for 'Query'", but this did work:
HttpContext.Current.Request.QueryString["index"]
I think a more elegant solution is to use the controller and the ViewData dictionary:
//Controller:
public ActionResult Action(int IFRAME)
{
ViewData["IsIframe"] = IFRAME == 1;
return View();
}
//view
@{
string classToUse = (bool)ViewData["IsIframe"] ? "iframe-page" : "";
<div id="wrap" class='@classToUse'></div>
}
For Asp.net Core 2
ViewContext.ModelState["id"].AttemptedValue
It was suggested to post this as an answer, because some other answers are giving errors like 'The name Context does not exist in the current context'.
Just using the following works:
Request.Query["queryparm1"]
Sample usage:
<a href="@Url.Action("Query",new {parm1=Request.Query["queryparm1"]})">GO</a>
If you are using .net core 2.0 this would be:
Context.Request.Query["id"]
Sample usage:
<a href="@Url.Action("Query",new {parm1=Context.Request.Query["queryparm1"]})">GO</a>
Source: Stackoverflow.com