How do I access data in a view in Razor?

跟風遠走 提交于 2020-01-15 10:53:24

问题


In standard WebForms, I would have a handful of properties, and set those. Then the Web Page would bind to them, for example with <%#CustomerName%>.

I understand that MVC is a different approach, but there must still be a basic need for displaying data from multiple sources at the same time.

I can see that if I use @Model then it will access the data set in the controller. But what if You want to display data from 2 or 3 separate items?

If I want to display a Customer, their address and their current order all on the same page, is there a way of passing these 3 seperate models to the one view, or is it a case that I need to create a partial view for each, and then display them all on the same page?


回答1:


Have a ViewModel which holds all those data(classes) as properties and return that to your view

public class CustomerViewModel
{
  public int ID { set;get;}
  public string Name { set;get;}
  public Address Address{ set;get;}
  public IEnumerable<Order> Orders { set;get;}

  public CustomerViewModel()
  {
    if(Address==null)
        Address=new Addres();
    if(Orders ==null)
        Orders =new List<Order>();

  }
}
public class Address
{  
   public string AddressLine1 { set;get;}
   //Other properties
}
public class Order
{  
   public int ORderID { set;get;}
   //Other properties
}

your GET Action method

public ActionResult Index(int id)
{
  var vm=new CustomerViewModel();

  vm.Name=repo.GetUser(id).Name;

  vm.Address=repo.GetAddressFromUserID(id);

  vm.Orders=repo.GetOrdersFromUser(id); 

  return View(vm);
}

And your View will be strongly typed tp CustomerViewModel

@model CustomerViewModel

<p>@Model.Name</p>
<p>@Model.Address.AddressLine1</>
@foreach(var item in Model.Orders)
{
  <p>@item.OrderID</p>
}



回答2:


Are the items related? If they are, then consider creating a ViewModel that is an aggregate of each model:

public CustomerViewModel
{
    public Customer Customer {get; set;
    public AddressViewModel Address {get; set;}
    public OrderViewModel Order {get; set;}
}

then in your view:

@inherits System.Web.Mvc.WebViewPage<CustomerViewModel>

@model CustomerViewModel

@Model.Customer.Name

@Model.Address.Street

@Model.Order.Name

@model.Order.Date

Otherwise, if the items are not related at all, then you can use the RenderAction:

In your Customer view:

@Model.Customer.Name

@(Html.RenderAction("GetAddress", "Customer", new { customerId = Model.Customer.Id });) 

@(Html.RenderAction("GetOrders", "Order", new { customerId = Model.Customer.Id });)



回答3:


What you would do is create a collection on your Model to contain the three customer models. When we are talking about Models in MVC, it is helpful to think of them as ViewModels. That is to say one model per view which will contain all of the data for that view.

So in the example below you have collection of customers from your data model set as a property on your view model. You can now iterate over @Model.Customers in your view (or in an Html helper extension method)

Example

public class MyViewModel{
    private List<Customer> _customers;

    public List<Customer> Customers{
        get{
            return _customers;
        }
    }
    //Other View Model Properties and Methods
}


来源:https://stackoverflow.com/questions/11635811/how-do-i-access-data-in-a-view-in-razor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!