views:

22

answers:

2

Hello there,

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_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 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

A: 

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.

eddiegroves
SMSAPP is my project name.i pass items as you said,return View(new MyViewModel(resultclass,resultsubject));built failed and got this error"The best overloaded method match for SMSAPP.Controllers.MyViewModel(SMSAPP.Models.SMSAPPDatacontext,SMSAPP.Models.SMSAPPDatacontext) has some invalid arguments "and head of view page appears asInherits="System.Web.Mvc.ViewPage<SMSAPP.Controllers.MyViewModel>" %>
hmmmmmm
I don't think your MyViewModel constructor has the correct type arguments. In Visual Studio hovering the mouse over 'var' should help. I'm guessing it should be something like IQueryable<tbl_classes> and IQueryable<tbl_subjects>.
eddiegroves
When i hover my mouse over 'var resultclass' it show "class System.Collection.Generic.List<T>".. .. when i hover my mouse over 'var resultsubject' it shows "interface System.Linq.IQueryable <out T>"............. Then i change my constructor arguments as you said....... public MyViewModel(IQueryable<tbl_class> resultclass, IQueryable<tbl_subject> resultsubject).... still got error of invalid arguments
hmmmmmm
A: 

sorry, wrong post

hmmmmmm