Get Query String Value in ASP.NET MVC Function - asp.net-mvc-4

I have an ASP.NET MVC app. My views use Razor. At the top of my CSHTML file, I have the following:
#functions
{
public static HtmlString IsSelectedCss(string name)
{
string selected = ""; // Need to get value of "t" from query string
HtmlString attribute = new HtmlString("");
if (selectedTab.Equals(name, StringComparison.InvariantCultureIgnoreCase))
{
attribute = new HtmlString("class=\"active\"");
}
return attribute;
}
}
I need this function to examine the query string. Specifically, I need to get the value of the "t" query string parameter. My challenge is, I cannot seem to figure out how to get access to the QueryString in this function.
How do I get the value of a query string parameter in a Razor function?
Thanks!

The query string can be gotten from below.
HttpContext.Current.Request.QueryString["t"]

You need to make your function non-static, since the querystring is part of the request.
You can then write
HttpContext.Request.Query["t"]

You should really be doing this in the controller and pushing it through the model. But if you insist, you can simply use:
<%= Request["t"] %>
But why not read it in your controller?!

Related

sitefinity ActionResult missing query string

I have widget and in the ActionResult method it get the query string as parameters. The widget takes the query string and calls a API. Is there anyway I can display a message to the view if its missing the query string?
Code
public ActionResult Index(string UserId, string BlogId)
Yep, do whatever you want
if(String.IsNullOrEmpty(UserId)){
// you get the idea
}
I would do one of the following
Add a property to the model, then handle that state in the view
Load a custom view that's just the message (cleanest)
Check for the empty querystring and do a return this.RedirectPermanent(url); to add the querystring to the page so there's no way it loads without something.

RazorPages anchor tag helper with multiple parameters

Here's the RazorPages page I'm trying to make a link to:
#page "{ReportId:int}/{SicCode:alpha?}"
This works
<a asp-page="/ReportSics" asp-route-ReportId="3">rs1</a>
it produces
rs1
But this produces a blank href.
<a asp-page="/ReportSics" asp-route-ReportId="3" asp-route-SicCode="10">rss2</a>
That is: the tag helper works with one parameter but not with two.
Why?
Is it possible to make it work?
(I have another page with the same #page but with the second parameter not optional and it appears to be impossible to create a link to it.)
Furthermore, requesting Page/2/M works, but Page/2/12 returns 404. Why? (The second parameter is a string that can sometimes be a number, but it always treated as a string.)
From the learn.microsoft.com webpage asp-all-route-data offers the following:
asp-all-route-data
The asp-all-route-data attribute supports the creation of a dictionary of key-value pairs. The key is the parameter name, and the value is the parameter value.
In the following example, a dictionary is initialized and passed to a Razor view. Alternatively, the data could be passed in with your model.
#{
var parms = new Dictionary<string, string>
{
{ "speakerId", "11" },
{ "currentYear", "true" }
};
}
<a asp-route="speakerevalscurrent"
asp-all-route-data="parms">Speaker Evaluations</a>
The preceding code generates the following HTML:
Speaker Evaluations
Extension: From here the parameters can be accessed either explicitly in the parameter list of the method:
public IActionResult EvaluationCurrent(int speakerId, string currentYear)
or as a collection (See response: queryString:
public IActionResult EvaluationCurrent()
{
var queryString = this.Request.Query;
}
This works
Yes it works because it produces a route that is similar to this baseUrl/reportsics/?reportId=5
And the other produces a URL that is similar to this baseUrl/reportsics/?reportId=5&sicCode=678 and then it doesn't match your route definition. I think you should try this.
Experimental
asp-page="/reportSics/#myId/#sicCode
Though this would not be the right way to do what you're thinking. If you really want to change your URL structure, why not do url-rewrite?
Edit.
Form your recent comments, seems you want to pass many parameters in your action method and not targeting URL structure. Then I recommend you just
public IActionResult(string ReportId, string sicCode)
{
//......
}
//And the your URL target
<a asp-page="ReportSics" asp-route-ReportId="55" asp-route-sicCode="566" ></a>
And then it will match the route. I think you should remove that helper you placed after your #page definition and try it out if this is what you have already done and the problem persists.
It turns out that if a parameter has the constraint :alpha then it only works if the value being passed can not be parsed as an int or float.

Expression for asp-route-{value}

asp-route-date="#{ DateTimeOffset.Parse(date); }"
Should that work? Can't find any info. Currently that somehow give me pure date variable value, f.e. - ...?date=01%2F02%2F2020+4%3A49+PM
Use Razor expressions instead Razor code blocks so that the value will be rendered in the resulting HTML, e.g.:
Razor:
<a asp-action="CheckDate" asp-route-date="#DateTimeOffset.Parse(date)">check</a>
Controller:
public ActionResult CheckDate(DateTimeOffset date)
{
// ...
}
The query string in your example seems fine, as the DateTimeOffset object needs to be encoded to be passed to the controller. For alternative formats and further information see How do I pass a datetime value as a URI parameter in asp.net mvc?.

Data is not inserting to the database

I am new to asp .net MVC 4.
I have one text box and the text box value I am fetching from one table.But while clicking on submit button this value I want to insert into different table , which is not inserting and showing error.It is taking value as null.
coding
View
#Html.TextBox("empname", (string)ViewBag.empname, new { #readonly = "readonly" })
controller
[HttpGet]
public ActionResult Facilities()
{
mstEmpDetail emp = new mstEmpDetail();
emp = db.mstEmpDetails.Single(x => x.intEmpId == 10001);
ViewBag.empname = emp.txtEmpFirstName;
return View();
}
[HttpPost]
public ActionResult Facilities(TrnBusinessCardDetail bc)
{
var empname1 = ViewBag.empname;
bc.txtfirstName = empname1;
db.TrnBusinessCardDetails.Add(bc);
db.SaveChanges();
return RedirectToAction("Facilities");
}
While I was working with normal text box it was inserting properly,but when I have retrieve
fro DB then i am getting this problem ?
How to solve this problem ?
Viewbag is a one way street - you can use it to pass information to the view, but you cannot use it to get the information from the view. The statement ViewBag.empname in your POST method has a value of null in your code.
As suggested by #dotnetom, ViewBag is a one way street. MVC is stateless so a POST request is not a "Round Trip" from previous get request. Thus your ViewBag can not hold its state.
MVC can determine (and construct) your action parameters from Form Parameters. In your case you have added a textbox with name "empname". So you should get this value as parameter in your POST request.
[HttpPost]
public ActionResult Facilities(TrnBusinessCardDetail bc, string empname)
{
bc.txtfirstName = empname;
db.TrnBusinessCardDetails.Add(bc);
db.SaveChanges();
return RedirectToAction("Facilities");
}
This would be simplest of solution given your problem. More appropriate would be binding your textbox directly with you model property. This way you will not have to worry about retrieving and assigning property value to model in your controller.
I think the problem is when you are using var empname1 = ViewBag.empname; in post controller because ViewBag.empname lost its value at that time.

pass decimal to controller mvc4

I'm having issues with one of my controller actions .
I have a decimal stored in my viewbag on my view. And am attempting to pass this via and actionlink to my controller method.
ViewBag.Interest =1.25
#Html.ActionLink("Export", "ExportInterest", "Export", new {id = ViewBag.Interest}, null).
My controller method looks something like this :
public ActionResult ExportInterest(decimal? id)
{
return View();
}
I can see the 1.25 be passed via query string but I'm getting a 404 file not found when it's being routed. Note: if I change it to just a whole number with no decimal point it's working fine. Is this an encoding error ? It's not recognising the decimal point , perhaps I need to escape it ? Is there a htmlhelper I should be using? Initially I thought it might be a localisation thing but I have my globalisation culture set up in my web.config. I'm obviously doing something silly here....any help would be appreciated...
Update: I have also tried casting my viewbag to a nullable decimal in the action link but this didn't have any effect
My guess is has to do with the data type in the view bag.
I have passed decimals to controllers before so I know it can be done. But if you changed your link to be:
#Html.ActionLink("Export", "ExportInterest", "Export", new {id =1.25}, null).
Does it work?
Try that:
decimal? d = (decimal?)ViewBag.Interest;
#Html.ActionLink("Export", "ExportInterest", "Export", new {id = d}, null)
or
#Html.ActionLink("Export", "ExportInterest", "Export", new {id = (decimal?)ViewBag.Interest}, null)