Billa
Billa

Reputation: 5266

MVC Action Method with nullable parameter

I am calling a action with the parameter value. But it is not assigning parameter value to action method parameter.

    public ActionResult UserDetail(long? userId)
    {

    }

localhost/Admin/UserDetail/10 --> 10 is not passed to userId

But

localhost/Admin/UserDetail/?userId=10 --> This works

What causes the first url not working? Any help?

Update:

tried this in global.asax still not working

 routes.MapRoute("ExistSiteUser",
              "UserDetail/{userId}",
               new
               {
                   controller = "Admin",
                   action = "UserDetail",
                   // nothing optional 
               }
         );
        routes.MapRoute("NewSiteUser",
             "UserDetail",
              new
              {
                  controller = "Admin",
                  action = "UserDetail",
                  userId = UrlParameter.Optional 
              }
        );

Upvotes: 1

Views: 1570

Answers (2)

Harshit Tailor
Harshit Tailor

Reputation: 3281

Replace this userID to id :-

public ActionResult UserDetail(long? id)
    {

    }

Upvotes: 3

Ankush Jain
Ankush Jain

Reputation: 1527

change the parameter name to 'id' instead of 'userid' and then try....it should work then

public ActionResult UserDetail(long? id)
{

}

Upvotes: 3

Related Questions