Summary of various ways that ASP. NET MVC passes model to view

Source: Internet
Author: User

Summary of various ways that ASP. NET MVC passes model to view

There are several ways to pass data to a view, as follows:

    • ViewData
    • ViewBag
    • PartialView
    • TempData
    • ViewModel
    • Tuple

Scene:

In the View page, drop-down box to select the course trigger event, showing the teacher's schedule, the student's class table,

The relevant model:

1 Public     class Course 2     {3 public         int Id {get; set;} 4 public         string Name {get; set;} 5     } 6  7 Public     class Teacher 8     {9 public         int Id {get; set;} Ten public         string Name {get; set;} Public         list<course> Courses {get; set;}     }13 Public     class Student15 {+ public         int Id {get; set;} + Public         string Name {get; set;} Public         list<course> Courses {get; set;} +     }

First, using ViewData to pass multiple model

-homecontroller

1 public         ActionResult Index () 2         {3             viewdata["Courses"] = _repository. Getcourses (); 4             viewdata["Students"] = _repository. Getstudents (); 5             viewdata["Teachers"] = _repository. Getteachers (); 6             return View (); 7         

-home/index.cshtml

 1 @using mvcapplication1.models 2 @using System.Web.Helpers; 3 @{4 Layout = null; 5} 6 7 <! DOCTYPE html> 8 

@Html. Raw (Json.encode (viewdata["Students")) is to convert the model to a Json string, use System.Web.Helpers, reference the library to the project, and must set the " Copy to local property "is true, otherwise error.

Second, using ViewBag to transfer multiple model

-homecontroller

1 public ActionResult Viewbagdemo () 2 {3     viewbag.courses = _repository. Getcourses (); 4     viewbag.students = _repository. Getstudents (); 5     viewbag.teachers = _repository. Getteachers (); 6     

-home/viewbagdemo.cshtml

The drop-down frame traversal course is changed to:
@foreach (var course in viewbag.courses)

The Getteachertable () method is changed to:
var teachers = @Html. Raw (Json.encode (viewbag.teachers));

The Getstudenttable () method is changed to:
var students = @Html. Raw (Json.encode (viewbag.students));

Third, using partial views to pass multiple model

-homecontroller

 1 public ActionResult Partialviewdemo () 2 {3 list<course> courses = _repository. Getcourses (); 4 return View (courses); 5} 6 7 public ActionResult Studentstopvdemo (string coursename) 8 {9 Ienumerable<course> courses = _repository .                             Getcourses (); var Selectedcourseid = (from C in courses11 where c.name = = CourseName12 Select C.id). FirstOrDefault (); ienumerable<student> students = _repository. Getstudents (); var studentsincourse = students. Where (s = = S.courses.any (c = c.id = = Selectedcourseid)). ToList (); Partialview ("STUDENTPV", Studentsincourse),}17 public ActionResult Teacherstopvdemo (String Co Ursename) {ienumerable<course> courses = _repository.                             Getcourses (); var Selectedcourseid = (from C in courses22 where c.name = = CourseName23 Select C.id). FirstOrDefault (); ienumerable<teacher> teachers = _repository. Getteachers (); teachersforcourse var = teachers. Where (t = = T.courses.any (c = c.id = = Selectedcourseid)).  ToList (); Partialview return ("TEACHERPV", Teachersforcourse); 27}

-HOME/PARTIALVIEWDEMO.CSHMTL

 1 @model ienumerable<mvcapplication1.models.course> 2 @{3 Layout = null; 4} 5 <! DOCTYPE html> 6 

-teacherpv.cshtml and Studentpv.cshtml

1 @model ienumerable<mvcapplication1.models.teacher> 2 <table id= "Tblteacherdetail" > 3     <tr> 4         <th> number </th> 5         <th> name </th> 6     </tr> 7     @foreach (var item in Model) 8     {9         <tr>10             <td> @item. Id</td>11             <td> @item. name</td>12         </tr>13     }14 </table>

Iv. using TempData to transfer multiple model

-homecontroller

 1 public ActionResult Tempdatademo () 2 {3//first read data from database into TempData, subsequent requests get data from TempData 4 tempdata["Courses"] = _ Repository. Getcourses (); 5//Let TempData save the data until the next request 6 Tempdata.keep ("Courses"); 7 return View (); 8} 9 Public ActionResult Teacherstempdata (string coursename) one {ten var courses = tempdata["Courses"] as Ienumerabl E&LT;COURSE&GT;;13//due to tempdata["Courses"] also to be the next request, continue to TempData save data tempdata.keep ("Courses"); var Selectedco Urseid = (from C in courses16 where c.name = = CourseName17 Select c.id). FirstOrDefault (); ienumerable<teacher> teachers = _repository. Getteachers (); var teachersforcourse = teachers. Where (t = = T.courses.any (c = c.id = = Selectedcourseid)). ToList (); return Partialview ("TEACHERPV", teachersforcourse);}22 All public ActionResult studentstempdata (string c Oursename) ($ var courses = tempdata["Courses"] as ienumerable<course>;26//due to tempdata["courses"] the next request Go onTempData Save Data Tempdata.keep ("Courses"); var Selectedcourseid = (from C in courses29 where c.name = = CourseName30 Select c.id). FirstOrDefault (); ienumerable<student> students = _repository. Getstudents (); var studentsforcourse = students. Where (s = = S.courses.any (c = c.id = = Selectedcourseid)). ToList (); Partialview return ("STUDENTPV", Studentsforcourse); 34}

-home/tempdatademo.cshtml

The drop-down box traverses the course:
@foreach (var course in Model)

Ajax Request Teacher Timetable:
@Url. Action ("Teacherstempdata", "Home")

Ajax requests the Student class table:
@Url. Action ("Studentstempdata", "Home")

V. Using ViewModel to pass multiple model

-view Model

1 public class SchoolVm2 {3 public     list<course> Courses {get; set;} 4 public     list<student> Students {get; set;} 5 public     list<teacher> Teachers {get; set;} 6}

-homecontroller

1 public ActionResult VIEWMODELDEMOVM () 2 {3     schoolvm vm = new SCHOOLVM (); 4     vm. Courses = _repository. Getcourses (); 5     VMs. Teachers = _repository. Getteachers (); 6     VMs. Students = _repository. Getstudents (); 7     return View (VM); 8}

-home/viewmodeldemovm.cshtml

@model MvcApplication1.Models.SchoolVm

The drop-down box traverses the course:
@foreach (var course in model.courses)

Ajax requests teacher schedules and student schedules:
@Html. Raw (Json.encode (model.teachers))
@Html. Raw (Json.encode (model.students))

Vi. passing multiple model using a tuple

-homecontroller

1 public ActionResult Tupledemo () 2 {3     var allmodels = new Tuple<list<course>, List<teacher>, list< Student>> (_repository. Getcourses (), 4         _repository. Getteachers (), _repository. Getstudents ()) {};5     return View (Allmodels); 6}

-home/tupledemo.cshtml

@model Tuple <list <mvcapplication1.models.course>, list <mvcapplication1.models.teacher>, List < Mvcapplication1.models.student>>

The drop-down box traverses the course:
@foreach (var course in model.item1)

Ajax requests teacher schedules and student schedules:
@Html. Raw (Json.encode (MODEL.ITEM2))
@Html. Raw (Json.encode (MODEL.ITEM3))

Reference Link: Https://www.codeproject.com/Articles/687061/Using-Multiple-Models-in-a-View-in-ASP-NET-MVC-M

Category: ASP. NET MVC Technology

Summary of various ways that ASP. NET MVC passes model to view

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.