Friday, August 8, 2014

MVC Pass Multiple Models to View

Mvc Tuple

In mvc three are tow way pass model
(1) View Model
2)Touple

Usually view models have metadata associated with them which among other allows you to perform validation and integrates with editor/display templates. Tuples don't seem appropriate. Another disadvantage is that they express less clearly the purpose of each property in the view model (Model.Item1Model.Item2, ...,
To use the tuple you need to do the following, in the view change the model to:
@model Tuple<List,Order>
to use @html methods you need to do the following i.e:
@Html.Editfor(tuple => tuple.Item2.OrderId)
or
    @foreach(var item in model.Item1)
    
    {
    
    
  • @item.name
  • }
    Item1 indicates the first parameter passed to the Tupel method and you can use Item2 to access the second model and so on.
    in your controller you need to create a variable of type Tuple and then pass it to the view:
        public ActionResult ItemOrderDetails(int id)
        {
            Order orderrep = _OrderRepository.GetOrder(Id)
            list _itemlist=_itemsRepository(id)
    if (orderrep == null)        {
                return HttpNotFound();
            }
            var tuple = new Tuple<List, Order>(_itemlist,orderrep );
    
            return View(tuple);
        }
    this way touple pass to view 

    No comments:

    Post a Comment

    http://blogsiteslist.com