Wednesday, November 29, 2017

How to use two object in a single view

There are multiple way to do is

1. Using ExpandoObject

ExpandoObject (the System.Dynamic namespace) is a class that was added to the .Net Framework 4.0 that allows us to dynamically add and remove properties onto an object at runtime.

 public ActionResult Index()  
    {  
        ViewBag.Message = "Welcome to my demo!";  
        dynamic mymodel = new ExpandoObject();  
        mymodel.Teachers = GetTeachers();  
        mymodel.Students = GetStudents();  
        return View(mymodel);  
    }  

In View

@using MultipleModelInOneView;  
@model dynamic  
@{  
    ViewBag.Title = "Home Page";  
}


2. Using View Model


ViewModel is nothing but a single class that may have multiple models. It contains multiple models as a property. It should not contain any method.

public class ViewModel  
{  
    public IEnumerable Teachers { get; set; }  
    public IEnumerable Students { get; set; }  




Controller code

public ActionResult IndexViewModel()  
{  
    ViewBag.Message = "Welcome to my demo!";  
    ViewModel mymodel = new ViewModel();  
    mymodel.Teachers = GetTeachers();  
    mymodel.Students = GetStudents();  
    return View(mymodel);  
}  

In View

@using MultipleModelInOneView;  
@model ViewModel   
@{  
    ViewBag.Title = "Home Page";  
}  

3. Using ViewData 


Controller Code

public ActionResult IndexViewData()  
{  
    ViewBag.Message = "Welcome to my demo!";  
    ViewData["Teachers"] = GetTeachers();  
    ViewData["Students"] = GetStudents();  
    return View();  
}  

View Code

@using MultipleModelInOneView;  
@{  
    ViewBag.Title = "Home Page";  


4. Using ViewBag


public ActionResult IndexViewBag()  
{  
    ViewBag.Message = "Welcome to my demo!";  
    ViewBag.Teachers = GetTeachers();  
    ViewBag.Students = GetStudents();  
    return View();  
}  

5. Using Tuple


A Tuple object is an immutable, fixed-size and ordered sequence object. It is a data structure that has a specific number and sequence of elements. The .NET framework supports tuples up to seven elements.

Using this tuple object we can pass multiple models from the controller to the view.

Controller Code

public ActionResult IndexTuple()  
{  
    ViewBag.Message = "Welcome to my demo!";  
    var tupleModel = new Tuple, List>(GetTeachers(), GetStudents());  
    return View(tupleModel);  
}  

View Code

@using MultipleModelInOneView;  
@model Tuple , List >  
@{  
    ViewBag.Title = "Home Page";  
}  

6. Using Render Action Method


A Partial View defines or renders a partial view within a view. We can render some part of a view by calling a controller action method using the Html.RenderAction method. The RenderAction method is very useful when we want to display data in the partial view. The disadvantages of this method is that there are only multiple calls of the controller.

Controller Code

///   
/// Render Teacher List  
///
  ///   
public PartialViewResult RenderTeacher()  
{  
    return PartialView(GetTeachers());  
}  
   
///   
/// Render Student List  
///
  ///   
public PartialViewResult RenderStudent()  
{  
    return PartialView(GetStudents());  
}  

View Code

@{  
   ViewBag.Title = "PartialView";  

@ViewBag.Message

  

  

    @{  
        Html.RenderAction("RenderTeacher");  
        Html.RenderAction("RenderStudent");  
    }  


RenderTeacher.cshtml
RenderStudent.cshtml

http://www.c-sharpcorner.com/UploadFile/ff2f08/multiple-models-in-single-view-in-mvc/

No comments:

Followers

Link