开发者

Pass multiple data items from controller to view

I am using VS 2010, MVC, VS 2005

I create .dbml file as my model and map tables inside .dbml file

i join tables using LINQ to SQL. I want to display record of two tables i.e. tbl_class, tbl_subject

COde in my controller looks like this

        public ActionResult SubjectByTeacher()
    {
        var DataContext = new SMSAPPDataContext();
        var resultclass = (from t in DataContext.tbl_teachers
                          from e in DataContext.tbl_teacherenrol开发者_如何转开发lments
                          from b in DataContext.tbl_batches
                          from c in DataContext.tbl_classes
                          from s in DataContext.tbl_subjects
                          where
                          t.Teacher_ID == e.Teacher_ID
                          &&
                          e.Batch_ID == b.Batch_ID
                          &&
                          b.Class_ID == c.Class_ID
                          &&
                          e.Sub_ID == s.Sub_ID
                          &&
                          t.Teacher_Name == "ABC"
                          select c;

        var resultsubject = from t in DataContext.tbl_teachers
                            from e in DataContext.tbl_teacherenrollments
                            from b in DataContext.tbl_batches
                            from c in DataContext.tbl_classes
                            from s in DataContext.tbl_subjects
                            where
                            t.Teacher_ID == e.Teacher_ID
                            &&
                            e.Batch_ID == b.Batch_ID
                            &&
                            b.Class_ID == c.Class_ID
                            &&
                            e.Sub_ID == s.Sub_ID
                            &&
                            t.Teacher_Name == "ABC"
                            select s;

            return View();

    }

Then i create a class in controller to map above two variables i.e. resultclass, resultsubject

    public class MyViewModel 
{
    public MyViewModel(SMSAPPDataContext resultclass, SMSAPPDataContext resultsubject)
    {
        this.rc = resultclass;
        this.rs = resultsubject;
    }
    public SMSAPPDataContext rc { get; private set; }
    public SMSAPPDataContext rs { get; private set; }
}

This class will be used in creating view as model in strongly typed view.

But i cant figure it out, what to pass in return view ?????

It may be just like i.e. return view(new myviewmodel);

But this gives error, Should i use ToList() property any where in code ???

If any one can told me any other way to do this, please help

Regards


Assuming your view is strongly typed to use MyViewModel, e.g:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<YourNameSpaceHere.MyViewModel>" %>

Then using something like

return View(new MyViewModel(resultclass, resultsubject));

In the controller should work. Although I don't think SMSAPPDataContext is the correct type that the LINQ query will be returning.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜