【问题标题】:Can a MVC strongly typed view display data from multiple databases with identical schemas using the same linq to sql dbml?MVC 强类型视图是否可以使用相同的 linq to sql dbml 显示来自具有相同模式的多个数据库的数据?
【发布时间】:2010-05-14 18:39:24
【问题描述】:

我使用强类型视图构建了一个入门 MVC 应用程序,通过将 linq 的连接字符串更改为 sql dbml 以在两个 dbs 之间切换并在调试中显示来自同一视图(索引/列表)中的两个相同数据库的表数据模式我可以看到发生了变化,但视图没有改变以反映新数据。我按照 nerddinner 教程中的步骤操作,并使用接口/存储库来检索数据。这在使用强类型视图和 linq to sql dbml 模型的 MVC 应用程序中是否可行?

【问题讨论】:

    标签: asp.net-mvc


    【解决方案1】:

    您应该能够创建 ViewModel,将视图绑定到 ViewModel,然后只需将数据选择到 ViewModel 的实例中即可。如果您这样做,那么数据来自数据库、XML 文件还是常量都无关紧要。看看this blog post 如何将 LINQ 结果放入强类型对象中(您可以在 ViewModel 中这样做)。

    这是一个例子:

        IEnumerable<MyViewModel> q = from c in Customers
                where c.firstname == "John"
                select new MyViewModel(c.company, c.lastname);                      
    

    【讨论】:

      猜你喜欢
      • 2022-06-21
      • 1970-01-01
      • 2016-09-10
      • 2012-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-19
      • 2019-01-09
      相关资源
      最近更新 更多