我是 MVC 的新手,我参与了一个用 ASP.NET MVC 1.0 开发的项目。我在 JavaScript 方面也很弱:-(
我试图演示 Master-Details View 如何在 Northwind 数据库中的“Orders”和“Order Details”表上工作。 (因此:表格有关系,即一个订单可以有多个订单详细信息)
我创建了两个控件(第一个用于 Orders,第二个用于 OrderDetails)。我将 Orders 表中的所有订单显示到 ListView 中。单击其中一个订单后,它会将我带到该订单的详细信息 View 。
我想要做的(& 失败)是在具有该订单的所有订单详细信息的订单的详细信息 View 下方创建一个 subview 。
我还想根据主 View 中的选择更改 subview 的内容。我阅读了很多关于使用 AJAX 和 JSON 来动态更改它的内容,但我也没有这样做:'(
任何人都可以提供帮助并为我提供如何实现它的技术和代码?
最佳答案
您可以使用 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>
然后在您的
Orders\Detail.ascx
部分观点:Id: <%= Model.Id %><br />
Name: <%= Model.Name %><br />
Description: <%= Model.Description %><br />
etc
关于asp.net-mvc - 主从 View ASP.NET MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1530495/