Master-Detail查看ASP.NET MVC

5 asp.net-mvc view master-detail

我是MVC的新手,我参与了一个用ASP.NET MVC 1.0开发的项目.我在JavaScript中也很弱:-(

我正在尝试演示Master-Details视图如何处理Northwind数据库中的"Orders"和"Order Details"表.(因此:表格有关系,即订单可以有多个订单明细)

我创建了两个控件(第一个用于订单,第二个用于OrderDetails).我将Orders表中的所有订单显示在List视图中.一旦我点击其中一个订单,它就会转到该订单的详细信息视图.

我想要做的(&失败)是在订单的详细信息视图下创建一个子视图,该视图包含该订单的所有订单详细信息.

我还想根据主视图中的选择更改子视图的内容.我阅读了很多关于使用AJAX和JSON来动态改变它的内容,但我也做不到:'(

任何人都可以提供帮助,并为我提供如何实现它的技术和代码?

Kei*_*ith 5

你可以使用MVC和jQuery轻松地完成这项工作.

首先在您Orders\List.aspx看来:

<script>
    // once the page has loaded
    $(function() {
        // set up your click event to load data
        $('.list-item').click(function() {
            // ajax load the content returned by the detail action
            $('#detail').load('<%= Url.Action("Detail") %>', { id: this.id } );
        });
    });
</script>

<style> .list-item { cursor: pointer; } </style>

<% // loop through the orders in your model and show them 
// as each div has the class list-item it will be give the click event
foreach( var order in Model ) { %>
    <div id="<%= order.Id %>" class="list-item"><%= order.Name %></div>
<% } %>

<%-- the panel that the ajaxed content will be loaded into --%>
<div id="detail"></div>
Run Code Online (Sandbox Code Playgroud)

然后在你的Orders\Detail.ascx局部视图中:

Id: <%= Model.Id %><br />
Name: <%= Model.Name %><br />
Description: <%= Model.Description %><br />
etc
Run Code Online (Sandbox Code Playgroud)