SOLUTION
the route
routes.MapRoute(
"Whatever", // Route name
"{controller}/{action}/{id}", //{ID} MUST BE USED IN YOUR CONTROLLER AS THE PARAMETER
new { controller = "MyController", action = "MyActionName", id = UrlParameter.Optional } // Parameter defaults
);
And that’s it! I guess you must use the name of the id in the
public actionresult(int id //must be ID HERE like global.asax)
Steps to reproduce my problem:
1. Create a new mvc3 application
2. Go to home controller and put Index(int? x){ return view()}
3. Run the application
4. Go to the url type in the url http://localthost:someport/Home/Index/1
5. Insert a break point in controller to see the value of x
6. Notice that even if you put the url above x NOT equal to 1 as is suppose to!
I just dont understand why i am getting a null in the id….
public ActionResult MyActionName(int? id)
{
//the id is null!!!!!!!!!!!! event thought i just entered the url below!
}
I enter the following url in my browser
http://locahost/MyController/MyActionName/1
//I also put this in my global.asax but it doesnt really help.
routes.MapRoute(
"Whatever", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "MyController", action = "MyActionName", id = UrlParameter.Optional } // Parameter defaults
);
AND!
My error if I put
public ActionResult MyActionName(int id)
{
//the id is null!!!!!!!!!!!! event thought i just entered the url below!
}
Note that the above example was made for simplicity this error is for the actual application.
Server Error in ‘/’ Application.
The parameters dictionary contains a null entry for parameter ‘MvrId’ of non-nullable type ‘System.Int32’ for method ‘System.Web.Mvc.ActionResult Index(Int32)’ in ‘MedicalVariance.Controllers.MedicineManagementController’. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Parameter name: parameters
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentException: The parameters dictionary contains a null entry for parameter ‘MvrId’ of non-nullable type ‘System.Int32’ for method ‘System.Web.Mvc.ActionResult Index(Int32)’ in ‘MedicalVariance.Controllers.MedicineManagementController’. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Parameter name: parameters
Make sure your route is defined before the default route.