Home MVC parameter returning null value
Reply: 0

MVC parameter returning null value

user5413
1#
user5413 Published in September 19, 2018, 9:35 am

Below is the Controller code:

public class HomeController : Controller
{
    //
    // GET: /Home/

    public string Index(string name)
    {
        return "Welcome to MVC_Demo"+name;
    }

}

and below is the Global.asax.cs codes:

public class MvcApplication : System.Web.HttpApplication
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

    }

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        RegisterRoutes(RouteTable.Routes);
    }
}

When I run the application and browse for (http://localhost/MVC_Demo/home/index/pradeep) it shows only, "Welcome to MVC_Demo" as the output, and not as "Welcome to MVC_Demo Pradeep" i.e the parameter name "Pradeep" is not getting displayed.

Considering me just a beginner any help would be highly appreciated.


share|improve this question
  • 3
    Because you method need to be public string Index(string id) so that you match your route (which expect a parameter named id). Or you need to create a specific route. – Stephen Muecke Apr 17 at 5:58

1 Answer 1

active oldest votes
up vote 2 down vote
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.486779 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO