Reputation: 2561
I have installed Visual Studio 2015 [free one on my Win 7] and just started evaluating its cool features. Trying to use view data like this:
public class VandVController : Controller
{
// GET: VandV
public ActionResult Index()
{
return View();
}
public ActionResult DisplayName(string name)
{
ViewData["name"] = name;
return View();
}
}
My View page looks like this:
@{
ViewBag.Title = "DisplayName";
}
<h2>Welcome to MVC training Mr. @ViewData["name"]</h2>
From my URL, when I'm using like this:
http://localhost:54748/VandV/DisplayName/abc
its displaying me: Welcome to MVC training Mr. where as it should display : Welcome to MVC training Mr. abc.
Here is my route:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Could any one please tell me whats going wrong? How can I display correct sting? Thanks.
Upvotes: 0
Views: 376
Reputation: 829
Add another route path to your configuration (change the route path name) and change the parameter name example as below
routes.MapRoute(
"Test", // Route name
"{controller}/{action}/{name}", // URL with parameters
new { controller = "Home", action = "Index", name = UrlParameter.Optional } // Parameter defaults
);
Upvotes: 1
Reputation: 2561
Modified like below and working.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{name}",
defaults: new { controller = "Home", action = "Index", name = UrlParameter.Optional }
);
}
Upvotes: 1
Reputation: 645
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Use new
{controller="VandV" ,action="DisplayName",id = UrlParameter.Optional}
Upvotes: 0