1
I’m trying to get into a controller, receive a parameter and print it in the View by a ViewData or ViewBag.
My Controller:
public ActionResult Index(string information)
{
ViewData["Bag"] = information;
return View();
}
My View:
@ViewData["Bag"]
The problem is that the parameter information is null every time I pass the URL call for example: http://localhost/Home/Index/teste.
routes.MapRoute( name:"Default1", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
This is the standard route.
you need this parameter? if yes you can put
string?to allow this to come null to the controller– jbrunoxd
i need you to take yes, but when I put the url http://localhost/Home/Index/test Asp.net-mvc it should take the string information = "test"
– Andre Alves
How is your file
RouteConfig? I’m asking because you posted Actionindexfrom your controller, and is accessingHome/Index/Teste. If your route is wrong, you need to access it like this:Home/Index?information=Teste– Randrade
I believe it’s the way Randrade wrote, which is the default route behavior, so you’d get the value
Testefor the variableinformationcorrectly in your Actionresult.– jbrunoxd
the path is default Routes.Maproute( name:"Default1", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = Urlparameter.Optional });
– Andre Alves
Andre, creates a Model, adds the Model reference in the view and the controller, fills in the Model. It should work.
– Andre Mesquita