Consider this scenario:
I want to create an MVC application for the Northwind database. I want to have a view that lists some orders, and I want to create links for CustomerID and EmployeeID and a link to details for [OrderDetails] so that when a user clicks on any of these links, related data appears on the same page.
My problem is how to separate this data, which is interconnected with the views and controllers that show the related data on the page, and the controllers responsible for individual pieces of information.
Also, how do I configure routing for this sample?
<table width="500px"> <tr> <th></th> <th> OrderID </th> <th> CustomerID </th> <th> EmployeeID </th> <th> OrderDate </th> <th> RequiredDate </th> <th> ShippedDate </th> <th> ShipVia </th> <th> OrderDetails </th> </tr> <% foreach (var item in Model) { %> <tr> <td> <%: Html.ActionLink("Edit", "Edit", new { id=item.OrderID }) %> | <%: Html.ActionLink("Details", "Details", new { id=item.OrderID })%> | <%: Html.ActionLink("Delete", "Delete", new { id=item.OrderID })%> </td> <td> <%: item.OrderID %> </td> <td> <%: item.CustomerID %> </td> <td> <%: item.EmployeeID %> </td> <td> <%: String.Format("{0:g}", item.OrderDate) %> </td> <td> <%: String.Format("{0:g}", item.RequiredDate) %> </td> <td> <%: String.Format("{0:g}", item.ShippedDate) %> </td> <td> <%: item.ShipVia %> </td> <td> <%: Html.ActionLink("OrderDetails", "GetOrderDetails", new { id = item.OrderID })%> </td> </tr> <% } %> </table> <p> <%: Html.ActionLink("Create New", "Create") %> </p> <div> <p> <% Html.RenderPartial("GetOrderDetails"); %> </p> <%--<uc1:GetOrderDetails ID="GetOrderDetails1" runat="server" />--%> </div>
and partial view of the order detail:
<table> <tr> <th></th> <th> OrderID </th> <th> ProductID </th> <th> UnitPrice </th> <th> Quantity </th> <th> Discount </th> </tr> <% foreach (var item in Model) { %> <tr> <td> <%: Html.ActionLink("Edit", "Edit", new { id=item.OrderID }) %> | <%: Html.ActionLink("Details", "Details", new { id=item.OrderID })%> | <%: Html.ActionLink("Delete", "Delete", new { id=item.OrderID })%> </td> <td> <%: item.OrderID %> </td> <td> <%: item.ProductID %> </td> <td> <%: String.Format("{0:F}", item.UnitPrice) %> </td> <td> <%: item.Quantity %> </td> <td> <%: item.Discount %> </td> </tr> <% } %> </table> <p> <%: Html.ActionLink("Create New", "Create") %> </p>
global.asax:
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Default2", // Route name "{controller}/{action}/{OrderId}/{CustomerID}", // URL with parameters new { controller = "NorthwindOrders", action = "Index", OrderId = UrlParameter.Optional, CustomerID = UrlParameter.Optional } // Parameter defaults ); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults ); }
Given this structure; How can I display order details in the order list when I click any of the OrderDetails links?
And why the url is displayed like this when I click on OrderDetails links:
http:
I want the url to display as follows:
http: