简体   繁体   English

新视图或部分视图

[英]New View or Partial View

This is the situation: 情况就是这样:

I have an ASP.NET MVC 4 application. 我有一个ASP.NET MVC 4应用程序。 When I run the application I go to a page and the Index - action on the controller takes a Guid as id - parameter. 当我运行应用程序时,我转到一个页面,控制器上的索引 - 操作采用Guid作为id - 参数。 With that id I get a list of items from a database and put it into a ViewModel. 使用该id,我从数据库中获取项目列表并将其放入ViewModel中。 This ViewModel is passed to a View which lists all of the items as an ActionLink (could be changed if necessary). 此ViewModel传递给View,该视图将所有项目列为ActionLink(如有必要,可以更改)。 When I click one of the items I want to get a list of other items, based on the id of the selected link, and show this new list right next to the first list. 当我单击其中一个项目时,我想根据所选链接的ID获取其他项目列表,并在第一个列表旁边显示此新列表。

But this is my question (and where I've been stuck for 2 days): What is the best way to do this? 但这是我的问题(我被困2天的地方):最好的方法是什么? Go to a new page or use a partial view. 转到新页面或使用部分视图。 Because I have been trying a bit of both and clearly I must've done some things wrong because none seemed to work. 因为我一直在尝试两者并且显然我必须做错事,因为似乎都没有。 I don't need any AJAX-stuff or helpers like that, only a correct way to get this done... :) 我不需要任何像这样的AJAX东西或助手,只有正确的方法才能完成这个...... :)

Thanks in advance! 提前致谢!

Update: the code that I already have 更新:我已经拥有的代码

View 视图

@foreach (var order in Model.Orders)
{
    <p>@Html.ActionLink(order.Name,
                        "OrderItems",
                        "OrdersController",
                        new { id = order.Id },
                        null)
    </p>
}

@if (Model.Detail != null)
{
    @Html.Partial("_OrderItemsByOrder", Model)
}

Controller 调节器

public ActionResult Index(Guid id)
{
    var orders = Services.GetOrders(id);
    var viewModel = new OrdersViewModel { Orders = orders };
    return View(viewModel);
}

public ActionResult OrderItems(Guid id, OrdersViewModel model)
{
    var orderItems = Services.GetOrderLines(id);
    var viewModel = new OrdersViewModel
    {
        Orders = model.Orders,
        Detail = new OrderDetailViewModel { OrderItems = orderItems }
    };
    return PartialView(viewModel);
}

You'll likely (for user usability) want to use AJAX and pull down the list of the "other items" from an AJAX response (JSON likely, or Partial HTML page if you really want to avoid javascript) 您可能(为了用户可用性)想要使用AJAX并从AJAX响应中下拉“其他项目”列表(如果您真的想避免使用javascript,可能会使用JSON或部分HTML页面)

If you really don't want to use AJAX, you could pull all of the items, and all of the "other items" and store them in "hidden" fields initially and do two select boxes. 如果你真的不想使用AJAX,你可以拉出所有项目和所有“其他项目”,并将它们存储在“隐藏”字段中,并做两个选择框。

http://www.plus2net.com/javascript_tutorial/dropdown-list.php http://www.plus2net.com/javascript_tutorial/dropdown-list.php

Example: http://www.plus2net.com/javascript_tutorial/dropdown-list-demo.php 示例: http//www.plus2net.com/javascript_tutorial/dropdown-list-demo.php

I would honestly go with the AJAX option though unless people will be using each of the dropdowns often, or if the amount of options is very limited. 老实说我会选择AJAX选项,除非人们经常使用每个下拉菜单,或者选项数量非常有限。

Ajax: http://www.plus2net.com/php_tutorial/ajax_drop_down_list.php Ajax: http//www.plus2net.com/php_tutorial/ajax_drop_down_list.php

And just want to point out, it doesn't need to be dropdown boxes that are used to select / populate, you'd just have to change the code around to populate a div or whatever element you want :) 而且只是想指出,它不需要是用于选择/填充的下拉框,你只需要改变代码来填充div或你想要的任何元素:)

There are several ways you could achieve a master/detail page like this, there really is no best way to do this. 有几种方法可以实现这样的主/详细页面,实际上没有最好的方法可以做到这一点。 AJAX may be the most elegant and user friendly, but it's by no means the "right" answer. AJAX可能是最优雅和用户友好的,但它绝不是“正确”的答案。

Looking at the code you posted, the simplest thing you could do is have one action method and one view. 查看您发布的代码,您可以做的最简单的事情是使用一个操作方法和一个视图。

public class OrdersViewModel
{
    public IEnumerable<Order> Orders { get; set; }
    public OrderItems SelectedOrderItems { get; set; }
}

public ActionResult Orders(Guid id, Guid? orderId)
{
    var model = new OrdersViewModel();
    model.Orders = Services.GetOrders(id);

    if (orderId != null)
    {
        model.SelectedOrderItems = Services.GetOrderLines(orderId);
    }

    return View(model);
}

The drawback with this rather basic approach is that you'll have possibly 2 Guids dirtying up your Url and this example doesn't verify that the orderId is actually owned by the (user?) Id - whatever the first Guid represents. 这个相当基本的方法的缺点是,你可能有2个Guids弄脏你的Url,这个例子不验证orderId实际上是由(用户?)ID所拥有 - 无论第一个Guid代表什么。 You could handle this with a few lines of code though. 你可以用几行代码来处理这个问题。

A more elegant way of handling this, if you don't mind your Url changing, is to stick with the second action method. 一个更优雅的处理方式,如果你不介意你的网址改变,就是坚持使用第二种动作方法。 I would hope you can determine the "owner" of an order detail from the detail itself, or in some way that wouldn't require you to pass this into the action method. 我希望您可以从细节本身确定订单明细的“所有者”,或者以某种方式确定不需要您将其传递给操作方法。 This helps ensure that your view only shows details from the right master. 这有助于确保您的视图仅显示来自正确母版的详细信息。

public ActionResult OrderDetails(Guid id /*the id of the order*/)
{
    var orderLines = Services.GetOrderLines(id);

    var model = new OrdersViewModel();
    //ideally you could get to the "owner id" of the order from the order lines itself
    //depending on how your domain model is set up
    model.Orders = Services.GetOrders(orderLines.Order.OwnerId); 
    model.SelectedOrderItems = orderLines;

    return View("Orders", model); //render the same view as the Orders page if like
}

Your view as listed in your question can largely stay the same. 您在问题中列出的观点可以大致保持不变。 Render the action links that represent all of the orders. 渲染表示所有订单的操作链接。 Then render the order details if there is one in the model: 然后渲染订单详细信息,如果模型中有一个:

@if (Model.SelectedOrderItems!= null)
{
    /* markup here or a render partial call */
}

If you are rendering the order details on other pages, you'll want to put your order details in a partial view so you don't duplicate any of your markup. 如果您要在其他页面上呈现订单详细信息,则需要将订单详细信息放在部分视图中,这样您就不会复制任何标记。 If it's only rendered here, then there's really no reason why you couldn't have your markup right in this view. 如果它只在这里呈现,那么你真的没有理由在这个视图中没有你的标记。

I would suggest something like this: 我会建议这样的事情:

  1. You have a controller with an action. 你有一个带动作的控制器。 The action takes two parameters, one with your guid and one optional with some other id 该操作有两个参数,一个是你的guid,一个是可选的,另一个是id
  2. You have your "mainview" that lists your items 您有“主视图”列出您的项目
  3. In your controller, add one other action, this time with only one parameter, an id 在你的控制器中,添加一个其他动作,这次只有一个参数,一个id
  4. Add another view, but this time, a partial view, that lists your other items 添加另一个视图,但这次是部分视图,列出了其他项目

Action 1 puts a list with items in the viewmodel, and an optional id to render some other items. 操作1在viewmodel中放置一个包含项目的列表,并在一个可选的id中放置一些其他项目。

Action 3 puts a list with some other items in the viewmodel 操作3在viewmodel中放置一个包含其他项的列表

Action 1 renders view 2 and action 3 renders view 4. View 2 renders, if the viewmodel says so, action 3. 操作1呈现视图2,操作3呈现视图4.视图2呈现,如果视图模型如此,则操作3。

Basically, when no specific item is selected then execution chain will be like this: 1 => 2 => Done 基本上,当没有选择特定项时,执行链将如下所示:1 => 2 =>完成

But when an item is selected the chain will be like this: 1 => 2 => 3 => 4 但是当选择一个项目时,链将是这样的:1 => 2 => 3 => 4

(Of course, this is just one way of doing things..) (当然,这只是做事的一种方式..)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM