I am new to C#.net MVC and am trying to add FullCalendar
to an MVC application.
The FullCalendar
script automatically adds ?start={}&end={}
to the URL...which is fine, but I have no idea how to use querystring
variables in a controller.
The methods I used in Webforms applications don't work. What do I need to add to the controller to access the querystring
variables?
Generally, the query string is one of client-side state management techniques in ASP.NET in which query string stores values in URL that are visible to Users. We mostly use query strings to pass data from one page to another page in asp.net mvc. In asp.net mvc routing has support for query strings in RouteConfig.
You can always use Request. QueryString collection like Web forms, but you can also make MVC handle them and pass them as parameters. This is the suggested way as it's easier and it will validate input data type automatically.
To pass in parameter values, simply append them to the query string at the end of the base URL. In the above example, the view parameter script name is viewParameter1.
public ActionResult SomeAction(string start, string end)
The framework will map the query string parameters to the method parameters.
I figured it out...finally found another article on it.
string start = Request.QueryString["start"];
string end = Request.QueryString["end"];
Davids, I had the exact same problem as you. MVC is not intuitive and it seems when they designed it the kiddos didn't understand the purpose or importance of an intuitive querystring system for MVC.
Querystrings are not set in the routes at all (RouteConfig). They are add-on "extra" parameters to Actions in the Controller. This is very confusing as the Action parameters are designed to process BOTH paths AND Querystrings. If you added parameters and they did not work, add a second one for the querystring as so:
This would be your action in your Controller class that catches the ID (which is actually just a path set in your RouteConfig file as a typical default path in MVC):
public ActionResult Hello(int id)
But to catch querystrings an additional parameter in your Controller needs to be the added (which is NOT set in your RouteConfig file, by the way):
public ActionResult Hello(int id, string start, string end)
This now listens for "/Hello?start=&end=" or "/Hello/?start=&end=" or "/Hello/45?start=&end=" assuming the "id" is set to optional in the RouteConfig.cs file.
If you wanted to create a "custom route" in the RouteConfig file that has no "id" path, you could leave off the "id" or other parameter after the action in that file. In that case your parameters in your Action method in the controller would process just querystrings.
I found this extremely confusing myself so you are not alone! They should have designed a simple way to add querystring routes for both specific named strings, any querystring name, and any number of querystrings in the RouteConfig file configuration design. By not doing that it leaves the whole use of querystrings in MVC web applications as questionable, which is pretty bizarre since querystrings have been a stable part of the World Wide Web since the mid-1990's. :(
Here is what I came up with. I was having major problems with this and believe I am in MVC 6 now but this may be helpful to someone even myself in the future..
//The issue was that Reqest.Form Request.Querystring and Request not working in MVC the solution is to use Context.Request.Form and also making sure the form has been submitted otherwise null reference or context issue bug will show up.
if(Context.Request.ContentLength != null)
{
String StartDate = Context.Request.Form["StartMonth"].ToString();
String EndMonth = Context.Request.Form["EndMonth"].ToString();
// Vendor
}
My problem was overwriting my query string parameters with default values:
routes.MapRoute(
"apiRoute",
"api/{action}/{key}",
new { controller = "Api", action = "Prices", key = ""}
);
No matter what I plugged into query string or how only key=""
results.
Then got rid of default overwrites using UrlParameter.Optional:
routes.MapRoute(
"apiRoute",
"api/{action}/{key}",
new { controller = "Api", action = "Prices", key = UrlParameter.Optional }
);
now
prices/{key}
or
prices?key={key}
both work fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With