当前位置: 动力学知识库 > 问答 > 编程问答 >

asp.net mvc - Sending multiple model to single view generating Error

问题描述:

I want to show the data form two different database table by using two model in one view.

My EnrolmentWithCourses method HomeController.cs

public ActionResult EnrolmentWithCourses()

{

List<object> myModelCollection = new List<object>();

myModelCollection.Add(db.Enrollments.ToList());

myModelCollection.Add(db.Courses.ToList());

return View(myModelCollection);

}

My view EnrolmentWithCourses.cshtml is:

 @model IEnumerable<object>

@{

List<ContosoUniversity.Models.Course> CourseList = Model.ToList()[0] as List<ContosoUniversity.Models.Course>;

List<ContosoUniversity.Models.Enrollment> EnrollmentList = Model.ToList()[1] as List<ContosoUniversity.Models.Enrollment>;

}

<h2>Course List</h2>

@foreach (var item in CourseList)

{

<li> @item.CourseID </li>

<li> @item.Title</li>

}

<h2>Course List</h2>

@foreach (var item in EnrollmentList)

{

<li> @item.EnrollmentID</li>

<li> @item.CourseID</li>

<

}

网友答案:

Your problem is that Model.ToList()[0] is a List<ContosoUniversity.Models.Enrollment> and not List<ContosoUniversity.Models.Course> because in your controller you add db.Enrollments.ToList() prior to db.Courses.ToList():

List<object> myModelCollection = new List<object>();
myModelCollection.Add(db.Enrollments.ToList());
myModelCollection.Add(db.Courses.ToList());
return View(myModelCollection);

if you change the order it will work:

List<object> myModelCollection = new List<object>();
myModelCollection.Add(db.Courses.ToList());
myModelCollection.Add(db.Enrollments.ToList());    
return View(myModelCollection);

BUT I strongly advice to create a stronglytyped model to avoid those kind of issue:

 public class YourModel
    {
       public IList<ContosoUniversity.Models.Enrollment> Enrollments {get;set}
       public IList<ContosoUniversity.Models.Course> Courses {get;set}
    }

Controller action will like:

public ActionResult EnrolmentWithCourses()
{
    var model = new YourModel{
        Enrollments = db.Enrollments.ToList(),
        Courses = db.Courses.ToList()
    };

    return View(model);
}

View

 @model YourModel

<h2>Course List</h2>
       @foreach (var item in Courses)
        {
             <li> @item.CourseID </li>
             <li> @item.Title</li>

        }
    <h2>Course List</h2>
     @foreach (var item in Enrollments)
        {
             <li> @item.EnrollmentID</li>
             <li> @item.CourseID</li>
            <
        }
网友答案:

You should create a composite Model like as follows :

public class EnrolmentWithCoursesModel
{
     List<ContosoUniversity.Models.Course> CourseList {get; set; }
     List<ContosoUniversity.Models.Enrollment> EnrollmentList {get; set; }
}

Now change your action to return the composite Model for view :

public ActionResult EnrolmentWithCourses()
{
            EnrolmentWithCoursesModel myModelCollection = new EnrolmentWithCoursesModel() { CourseList = db.Courses.ToList(), EnrollmentList =  db.Enrollments.ToList() };

            return View(myModelCollection);
}

Now, your view should change to Model to make EnrolmentWithCoursesModel

 @model EnrolmentWithCoursesModel

<h2>Course List</h2>
       @foreach (var item in Model.CourseList)
        {
             <li> @item.CourseID </li>
             <li> @item.Title</li>

        }
    <h2>Course List</h2>
     @foreach (var item in Model.EnrollmentList)
        {
             <li> @item.EnrollmentID</li>
             <li> @item.CourseID</li>
            <
        }
分享给朋友:
您可能感兴趣的文章:
随机阅读: