Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC Controller Action Parameter is null

I have Controller name: District and Action name: Incharges But I want the URL to be like this (action name with some paremeter)

www.example.com/district/incharges/aaa

www.example.com/district/incharges/bbb

www.example.com/district/incharges/ccc

But, while debugging teamName always return as NULL in the action parameter.

Routing

routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

            routes.MapRoute(
            "DistrictDetails",
            "District/Incharges/{teamName}",
            new { controller = "District", action = "Incharges" }
            ); 

Controller

But, while debugging teamName always return as NULL in the action parameter.

public class DistrictController : Controller
    {     


        public ActionResult Incharges(string teamName)
        {
            InchargePresentationVM INPVM = new InchargePresentationVM();
            INPVM.InitializePath(teamName, string.Empty);
            return View("", INPVM);
        }
}

View

@{
    ViewBag.Title = "Index";
}

<h2>Index About</h2>
like image 912
Velu Avatar asked Jan 29 '14 17:01

Velu


2 Answers

specific route you have to declare the first

routes.MapRoute(
            "DistrictDetails",
            "District/Incharges/{teamName}",
            new { controller = "District", action = "Incharges", id = UrlParameter.Optional }

routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );); 
like image 63
Ilya Avatar answered Nov 08 '22 11:11

Ilya


ASP.NET MVC DefaultModelBinder will try and do implicit type conversion of the values from your value provider , eg. form, to the action method parameters. If it tries to convert a type from the value provider to the parameter and it is not able to do it, it will assign null to the parameter.

Regarding routing, ASP.NET MVC has the concept of conversion over configuration. If you follow the conversion, then instead of configuration. You can keep your default route and always have the route you want by naming your controllers, action methods and parameter names.

With the convention over configuration you must keep the default HomeController which is the entry point of the application and then name other controllers as below. These can conform to the route names you want.

    namespace ConversionOverConfiguration
    {

      public class  DistrictController: Controller
      {
         public ActionResult Incharges(int aaa)
         {
            //You implementation here

             return View();
         }

      }

    }
The route will look as below if you have this conversion implementation
    //Controller/ActionMethod/ActionMethodParameter
    //District/Incharges/aaa

And this will give you domain URI:www.example.com/district/incharges/aaa . If action method parameter type is a string, then domain URI is:www.example.com/district/incharges/?aaa=name is a string. Then you can keep the ASP.NET MVC default routing

    routes.MapRoute
    (
      name: "Default",
      url: "{controller}/{action}/{id}",
      defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional      
    });
like image 23
Julius Depulla Avatar answered Nov 08 '22 12:11

Julius Depulla