Click here to Skip to main content
15,884,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new to the ASP.NET MVC. I have created the Home controller and action call showall with parameter but when it hit action parameter allways null
route map is default:
C#
routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }


            );


.................
C#
public ActionResult  showall (int? x)
{
    //this x is all ways null
 if(x==null)
 {
    ViewBag.id=0;
 }
else
 {
     ViewBag.id=x;
 }
    return View();

}
Posted
Updated 19-Jan-15 1:02am
v2

It seems because you are not using proper URL format in browser while calling that action. By default, while running the application URL would be something like "http://localhost:32215/" and in this case you will get Null for id.

If we write: http://localhost:32215/Home/index/2321321
Then I am getting 2321321 as Id. (Replace "localhost:32215" with your default address)

Thanks.
 
Share this answer
 
v2
Hello,

The id is always coming null because you may not be using properly to assign the id.

First of all the parameter you have written is int? x
The default behaviour of MVC route you have mentioned in the route is
id = UrlParameter.Optional . So you should have your parameter as id not x.

If you wish to use x, then you should access your url like:-
C#
/Home/showAll?x=123


Then you will see the value 123 when the action method debugger gets hit.
I hope you understand what I meant.
Please post back your queries if any.
Thanks
 
Share this answer
 
Comments
chathuranga abeyrathne 19-Jan-15 8:25am    
So how can i use 2 parameters in action and how to using the @Html.ActionLink()
[no name] 19-Jan-15 9:13am    
Yes you can use two or more parameters using & operator.
like /Home/Index?x=123&y=345
and the @Html.ActionLink("A name to Show","Action","Controller",new{all your parameters here})
chathuranga abeyrathne 19-Jan-15 11:52am    
Thanks It Working thanks again...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900