In MVC "Contoso University" tutorial by Tom Dykstra, "Reading Related Data with the Entity Framework in an ASP.NET MVC Application (5 of 10) ", in Views\Instructor\Index.cshtml, when displaying courses related to the selected
instructor, there is following code snippet:
@foreach (var item in Model.Courses) { string selectedRow = ""; if (item.CourseID == ViewBag.CourseID) { selectedRow = "selectedrow"; }
<trclass="@selectedRow"><td>
@Html.ActionLink("Select", "Index", new { courseID = item.CourseID })</td>
Please see the above underlined sentence. To my understanding, this sentence is generating a link to the Index action of Instructor controller passing in CourseID parameter. But when running the project, this sentence generated a link
like this: <a href="/Instructor/Index/12?courseID=2021">Select</a>. This link contains 2 action parameters: courseID(2021) and id(12). This id(12) is the id of the selected instructor.
My question is: the underlined @Html.ActionLink(...) only provided courseID parameter, but the generated link has another parameter id in addition to courseID. Can anyone explain to me how this id parameter is added to the link? Many
thanks.
In asp.net mvc you have to ways to store data in a URL, route data nd the querystring. The querystring is variables after ? and must be explicitly named. So ?courseid=12 is expliclity saying that courseid = 2.
Route data is when a parameter is embedded into the url itself. Look at the route table (in global.asax in mvc3, in RouteConfig.cs in MVC4) and you will see a line like this:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
That says that MVC should look a the URL and break up the url into components based on the url parameter second parameter. The url string "{controller}/{action}/{id} tells the framework how to figure out which controller, action and parameter called id to
set.
For example the url /Instructor/Index/12 can be broken up into
{controller}/{action}/{id}
Instructor / Index / 12
So
controller=Instructor
action = Index
id= 12
So in your url, because CourseId is not part of the route then it will show up in the querystring. Now you can add more variables to the route and in that case CourseId wouldn't show up.
GeorgeJ88
0 Points
4 Posts
Why did MVC ActionLink generate more parameter than provided
Jan 08, 2013 06:13 AM|LINK
In MVC "Contoso University" tutorial by Tom Dykstra, "Reading Related Data with the Entity Framework in an ASP.NET MVC Application (5 of 10) ", in Views\Instructor\Index.cshtml, when displaying courses related to the selected instructor, there is following code snippet:
@foreach (var item in Model.Courses) { string selectedRow = ""; if (item.CourseID == ViewBag.CourseID) { selectedRow = "selectedrow"; } <tr class="@selectedRow"> <td> @Html.ActionLink("Select", "Index", new { courseID = item.CourseID }) </td>
Please see the above underlined sentence. To my understanding, this sentence is generating a link to the Index action of Instructor controller passing in CourseID parameter. But when running the project, this sentence generated a link like this: <a href="/Instructor/Index/12?courseID=2021">Select</a>. This link contains 2 action parameters: courseID(2021) and id(12). This id(12) is the id of the selected instructor.
My question is: the underlined @Html.ActionLink(...) only provided courseID parameter, but the generated link has another parameter id in addition to courseID. Can anyone explain to me how this id parameter is added to the link? Many thanks.
George
CodeHobo
All-Star
18669 Points
2648 Posts
Re: Why did MVC ActionLink generate more parameter than provided
Jan 08, 2013 06:23 AM|LINK
In asp.net mvc you have to ways to store data in a URL, route data nd the querystring. The querystring is variables after ? and must be explicitly named. So ?courseid=12 is expliclity saying that courseid = 2.
Route data is when a parameter is embedded into the url itself. Look at the route table (in global.asax in mvc3, in RouteConfig.cs in MVC4) and you will see a line like this:
routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } );That says that MVC should look a the URL and break up the url into components based on the url parameter second parameter. The url string "{controller}/{action}/{id} tells the framework how to figure out which controller, action and parameter called id to set.
For example the url /Instructor/Index/12 can be broken up into
{controller}/{action}/{id}
Instructor / Index / 12
So
controller=Instructor
action = Index
id= 12
So in your url, because CourseId is not part of the route then it will show up in the querystring. Now you can add more variables to the route and in that case CourseId wouldn't show up.
Blog | Twitter : @Hattan
.NetBoy
Member
439 Points
116 Posts
Re: Why did MVC ActionLink generate more parameter than provided
Jan 08, 2013 06:29 AM|LINK
<a href="/Instructor/Index/12?courseID=2021">Select</a>
12 = Instructor ID ( on my understanding ) --- get appended to the URL, as its the current Instructor that was selected initially
courseID=2021 --- the course the instructor teaches
GeorgeJ88
0 Points
4 Posts
Re: Why did MVC ActionLink generate more parameter than provided
Jan 08, 2013 06:49 AM|LINK
Thanks to all the answers.
@Html.ActionLink(...) only specified one parameter. Who or what mechanism added another parameter (id=12)?
ignatandrei
All-Star
137682 Points
22147 Posts
Moderator
MVP
Re: Why did MVC ActionLink generate more parameter than provided
Jan 08, 2013 07:01 AM|LINK
Please show Action signature.
GeorgeJ88
0 Points
4 Posts
Re: Why did MVC ActionLink generate more parameter than provided
Jan 08, 2013 07:06 AM|LINK
Action signature as follows:
public ActionResult Index(Int32? id, Int32? courseID)
thanks
ignatandrei
All-Star
137682 Points
22147 Posts
Moderator
MVP
Re: Why did MVC ActionLink generate more parameter than provided
Jan 08, 2013 07:11 AM|LINK
provide the id as null also in your actionlink .
amitpatel.it
Star
8070 Points
1880 Posts
Re: Why did MVC ActionLink generate more parameter than provided
Jan 08, 2013 07:21 AM|LINK
Make action as below
@Html.ActionLink("Select", "Index", new { id= item.id,courseID = item.CourseID })MCPD Enterprise and Web Application
MCTS Web, Window and Enterprise Application
GeorgeJ88
0 Points
4 Posts
Re: Why did MVC ActionLink generate more parameter than provided
Jan 08, 2013 07:34 AM|LINK
But in the tutorial, the author only used this:
@Html.ActionLink("Select", "Index", new { courseID = item.CourseID })
and this sentence generated 2 parameters:courseID and id. (eg, <a href="/Instructor/Index/12?courseID=2021">Select</a>)