New view or partial view

In this situation:

I have an ASP.NET MVC 4 application. When I start the application, I go to the page, and the Index action on the controller takes the Guid parameter as id. With this id, I get a list of items from the database and put it in the ViewModel. This ViewModel is passed to the view, which lists all the elements as an ActionLink (can be changed if necessary). When I click one of the elements, I want to get a list of other elements based on the identifier of the selected link and show this new list next to the first list.

But this is my question (and where I got stuck for 2 days): What is the best way to do this? Go to a new page or use a partial view. Because I tried a little bit of both, and I must have done some things wrong, because no one seemed to work. I don't need any AJAX stuff or helpers, just the right way to do this ... :)

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); } 
+4
source share
3 answers

There are several ways to reach the home / detail page, such as this, there really is no better way to do this. AJAX may be the most elegant and user friendly, but this is by no means the โ€œrightโ€ answer.

By looking at the code you posted, the simplest thing you could do is to have one action method and one look.

 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 disadvantage of this rather basic approach is that you will probably have 2 Guides polluting your Url, and this example does not check if orderId really belongs to the identifier (user?) - regardless of what the first Guid represents. You can handle this with a few lines of code.

A more elegant way to handle this, if you don't mind changing your Url, is to stick with the second method of action. I hope you can determine the "owner" of the order part from the part itself or somehow it will not require you to pass this into the action method. This helps to ensure that your view shows only data from the right wizard.

 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 opinion stated in your question can remain largely unchanged. Display action links that represent all orders. Then visualize the order details, if they are in the model:

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

If you process order details on other pages, you want to place order details in a partial view so as not to duplicate any markup. If it appears only here, then there really is no reason why you could not have your markup in this view.

+2
source

You most likely (for your convenience) want to use AJAX and list the โ€œother elementsโ€ from the AJAX response (probably a JSON or Partial HTML page if you really want to avoid javascript)

If you really don't want to use AJAX, you can pull out all the elements and all the "other elements" and first save them in the "hidden" fields and make two selection fields.

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

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

I would honestly agree with the AJAX option, although people will often use each of the drop-down lists, or if the number of options is very limited.

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

And I just want to point out that there is no need for dropdown windows that are used to select / fill, you just need to change the code to fill in a div or any other element that you want :)

+3
source

I would suggest something like this:

  • You have a controller with an action. The action takes two parameters, one with your pointer and one additional with the other id
  • You have a "mainview" listing your items.
  • In your controller, add another action, this time with one parameter, id
  • Add another view, but this time a partial view listing your other items

Step 1 places a list with items in the viewmodel and an optional identifier to display some other items.

Step 3 puts a list with some other elements in the viewmodel

Step 1 displays view 2 and step 3 displays view 4. View 2 displays, if the view model says so, step 3.

Basically, when no specific element is selected, the execution chain will look like this: 1 => 2 => Done

But when an item is selected, the chain will look like this: 1 => 2 => 3 => 4

(Of course, this is just one way to do something ..)

+1
source

Source: https://habr.com/ru/post/1380595/


All Articles