【问题标题】:Pass multiple data items from controller to view将多个数据项从控制器传递到视图
【发布时间】:2014-10-03 08:26:47
【问题描述】:

我正在使用 VS 2010、MVC、VS 2005

我创建 .dbml 文件作为我的模型并在 .dbml 文件中映射表

我使用 LINQ to SQL 连接表。 我想显示两个表的记录 即 tbl_class, tbl_subject

我的控制器中的代码如下所示

        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();

    }

然后我在控制器中创建一个类来映射以上两个变量,即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; }
}

这个类将用于在强类型视图中创建视图作为模型。

但我想不通,在返回视图中传递什么?????

可能就像返回视图(new myviewmodel);

但这会产生错误,我应该在代码中的任何位置使用 ToList() 属性吗???

如果有人可以告诉我任何其他方式来做到这一点,请帮助

问候

【问题讨论】:

    标签: asp.net-mvc


    【解决方案1】:

    假设您的视图是强类型以使用 MyViewModel,例如:

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

    然后使用类似的东西

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

    在控制器中应该可以工作。虽然我不认为 SMSAPPDataContext 是 LINQ 查询将返回的正确类型。

    【讨论】:

    • SMSAPP 是我的项目名称。我按照你说的传递项目,返回 View(new MyViewModel(resultclass,resultsubject));构建失败并收到此错误“SMSAPP.Controllers.MyViewModel(SMSAPP.Models.SMSAPPDatacontext,SMSAPP.Models.SMSAPPDatacontext) 的最佳重载方法匹配有一些无效参数”并且视图页面的头部显示为 Inherits="System.Web. Mvc.ViewPage" %>
    • 我认为您的 MyViewModel 构造函数没有正确的类型参数。在 Visual Studio 中,将鼠标悬停在“var”上应该会有所帮助。我猜它应该类似于 IQueryable 和 IQueryable
    • 当我将鼠标悬停在“var resultclass”上时,它显示“class System.Collection.Generic.List”.. .. 当我将鼠标悬停在“var resultsubject”上时,它显示“ interface System.Linq.IQueryable "...... 然后我按照你说的那样改变我的构造函数参数............ public MyViewModel(IQueryable resultclass, IQueryable resultsubject).... 仍然出现无效参数错误
    猜你喜欢
    • 2014-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多