如何解耦关系型 MVC 数据

发布于 2024-11-07 13:04:06 字数 4691 浏览 4 评论 0原文

考虑这个场景:

我想为 Northwind 数据库构建一个 MVC 应用程序。我想要一个列出一些订单的视图,并且想要创建 CustomerID 和 EmployeeID 的链接以及 [OrderDetails] 的详细信息链接,以便当用户单击任何这些链接时,相关数据将显示在同一页面中。

我的问题是如何将这些相互关联的数据解耦到在页面中显示相关数据的视图和控制器,以及负责信息各个部分的控制器。

另外,如何为此示例配置路由?

<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>

和订单详细信息部分视图:

<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
        );
    }

给定此结构;单击任何 OrderDetails 链接时,如何在订单列表下显示订单详细信息?

而且,为什么当我单击 OrderDetails 链接时 URL 显示如下:

http://localhost:49444/NorthwindOrders/GetOrderDetails?id=10250

我希望 URL 显示如下:

http://localhost:49444/NorthwindOrders/GetOrderDetails/10250

Consider this Scenario:

I want to build an MVC application for Northwind Database. I want to have a view that list some orders and I want to create links for CustomerID and EmployeeID and a details link for [OrderDetails] so that when a user clicks any of these links related data will appear in the same page.

My problem is how to decouple this data that is interrelated into views and controllers that show related data in a page, and controllers responsible for individual parts of the 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 Order Details partial view:

<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 I can display order details under the order list when any of the OrderDetails links are clicked?

And, why is the URL displayed like this when I click on OrderDetails links:

http://localhost:49444/NorthwindOrders/GetOrderDetails?id=10250

I want the URL to display this way:

http://localhost:49444/NorthwindOrders/GetOrderDetails/10250

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ゝ杯具 2024-11-14 13:04:06

对于您的路由问题:

如果您像这样设置路由:

    routes.MapRoute(
        "orderdetails", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "NorthwindOrders", action = "GetOrderDetails", id = UrlParameter.Optional, CustomerID = UrlParameter.Optional } // Parameter defaults
    );

它将按照您想要的方式构建 URL。

(或者,您可以将 GetOrderDetails 操作的参数重命名为 string OrderId,它将找到按照您想要的方式格式化 URL 的路由。)

至于您的主要问题:

如何我是否根据链接点击“动态”加载页面上的内容?

有两种方法可以解决此问题:

  • 回发页面。
  • AJAX / 动态加载页面 HTML 中的数据和元素。

在回发场景中:

在这种场景中,您的链接将全部转到构建模型的操作,该模型包含主页中的订单列表,并且对于您单击的特定订单的详细信息,您将使用该订单的具体信息填充您的模型命令。然后,您的 ActionResult 返回的是相同的视图(或者至少看起来相同的视图),并且您的视图将使用 PartialView Html 帮助器来呈现详细信息。

OrderViewModel:

public class OrderViewModel
{
  public IList<Order> Orders {get;set;}
  public OrderDetail Details {get;set;}
}

OrderView.aspx:

<%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage<OrderViewModel>" %>
<%:Html.DisplayFor(m=>m.Orders)%> <%-- Displays the Table above listing orders --%>
<%:Html.DisplayFor(m=>m.Details)%> <%-- Displays your custom view of the details --%>

OrderController:

...
public ActionResult List()
{
  var model = new OrderViewModel { Orders = GetAllOrders(), Details = null };
  return View("OrderView", model);
}

public ActionResult GetOrderDetails(string OrderId)
{
  var model = new OrderViewModel { Orders = GetAllOrders(), Details = GetOrder(OrderId) };
  return View("OrderView", model);
}
...

在 Ajax 场景中:

Ajax 场景本质上是相同的,除了您在 ajax 调用中隐藏服务器往返,然后重新加载页面,或者只是一个包含您想要的内容的 div ajax 调用的返回数据中的 html(或 JSON)。

Ajax 方法的优点是您可以在不同的控制器上为要更新的页面的各个部分指定不同的操作。

For your routing question:

if you set up a route like so:

    routes.MapRoute(
        "orderdetails", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "NorthwindOrders", action = "GetOrderDetails", id = UrlParameter.Optional, CustomerID = UrlParameter.Optional } // Parameter defaults
    );

It will build the URL in the fashion you're wanting.

(Alternatively, you can rename the parameter to your GetOrderDetails action to string OrderId and it will find the route that formats the URL the way you want it.)

As far as your main question:

How do I 'dynamically' load content on the page based on clicks to links?

There are two ways to approach this:

  • Post back pages.
  • AJAX / Dynamic loading of data and elements in the HTML on your page.

In the post-back scenario:

In this scenario your links would all go to actions that build a model that includes the orderlist from your main page, and for the details of the specific order you clicked you'd populate your model with specifics for that order. Then your ActionResult return is the same view (or one that looks the same at least), and your view would use the PartialView Html helper to render the details.

OrderViewModel:

public class OrderViewModel
{
  public IList<Order> Orders {get;set;}
  public OrderDetail Details {get;set;}
}

OrderView.aspx:

<%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage<OrderViewModel>" %>
<%:Html.DisplayFor(m=>m.Orders)%> <%-- Displays the Table above listing orders --%>
<%:Html.DisplayFor(m=>m.Details)%> <%-- Displays your custom view of the details --%>

OrderController:

...
public ActionResult List()
{
  var model = new OrderViewModel { Orders = GetAllOrders(), Details = null };
  return View("OrderView", model);
}

public ActionResult GetOrderDetails(string OrderId)
{
  var model = new OrderViewModel { Orders = GetAllOrders(), Details = GetOrder(OrderId) };
  return View("OrderView", model);
}
...

In the Ajax scenario:

The Ajax scenario is essentially the same except that you hide the server round-trip in an ajax call, and then reload the page, or just a div with the content you want from the html (or JSON) in the return data of the ajax call.

The advantage of the Ajax approach is that you could specify a different Action on a different Controller for the various parts of the page you wanted to update.

坦然微笑 2024-11-14 13:04:06

尼玛,
您可以通过部分视图或渲染操作以及 json 来显示数据来完成
基本示例如下:
http://www.asp.net/mvc/tutorials /iteration-7-add-ajax-functionality-cs

或非常流行的选项是 jquery.dialog() 中任务的显示操作

取决于您想要继续的方式
首席采购官


revised:

我的不好。
从逻辑上看是这样的:
这仅取决于您,您想走哪条路。
(从我的角度来看)

分离逻辑的好处是:

  1. 易于理解,因为控制器仅与以下操作相关: 订单
  2. 如果需要,可以在更多地方使用
  3. 易于仅测试小项目,例如一个测试用例/夹具仅用于订单的控制器
  4. 对于更复杂的项目很有用

另一方面

  1. 将所有内容保留在一个控制器中并按区域分割逻辑一切都
  2. 在一起,很容易看到其他方法及其功能

希望这就是您正在寻找的
首席采购官

Nima,
you can do it via partial views or render action, and json to display data
the basic example is as example here:
http://www.asp.net/mvc/tutorials/iteration-7-add-ajax-functionality-cs

or very popular option is display action for the task in jquery.dialog()

depends on you what way you want to proceed
cpo


revised:

My bad.
From logic point of view, it is as this:
It depends only on you, which way you want to go.
(From my point of view)

Benefits of separating the logic is :

  1. simple to understand as controller is related only to actions for eg: orders
  2. Can be used on more places if you need
  3. Easy to test only small items, eg one testcase/fixture to controller for only orders
  4. Usefull for more complicated projects

On the other hand

  1. Keep everything in one controller and split the logic by regions
  2. Eveything is together, easy to see other methods and what they do

Hope this is what you were looking for
cpo

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文