MVC LINQ to SQL 表连接记录显示 [英] MVC LINQ to SQL Table Join Record Display

查看:20
本文介绍了MVC LINQ to SQL 表连接记录显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将视图数据传递给用户控件时,我在向我的视图显示记录时遇到问题.这仅适用于我使用表连接的 linq to sql 对象.

我收到的异常是无法转换类型为 '<>f__AnonymousType410[System.String,System.Int32,System.Nullable1[System.DateTime],System.String 的对象,System.String,System.String,System.String,System.String,System.Nullable1[System.Single],System.Nullable1[System.Double]]' 输入 App.Models.table1."

我已经搜索了此问题的修复程序,但对这里的问题不太熟悉,无法搜索正确的主题.这在理论上应该有效,这适用于单表检索,但是当我在它们中添加连接时,我遇到了问题.我目前正在使用 foreach 语句通过单表声明查询我的数据.任何帮助将不胜感激.提前致谢.

我目前的设置是:

CViewDataUC.cs(我的类专门为用户控件保存视图数据和数据连接)

public void Info(ViewDataDictionary viewData, int id){var dataContext = new testDataContext();var info = from table1 in dataContext.table1在 table1.type_id 上的 dataContext.table2 中加入 table2 等于 table2.type_id在 table1.id 上的 dataContext.table3 中加入 table3 等于 table3.id在 table1.id 上的 dataContext.table4 中加入 table4 等于 table4.id其中 table1.id == id选择新的{表1.column1,表1.column2,表1.column3,表1.column4,表1.column5,表1.column6,table1.column7,表2.column1,table3.column1,表4.column1};viewData["vd_Info"] = 信息;}

HomeController.cs(控制器)

public ActionResult Information(int id){ViewData["Title"] = "信息";CViewDataUC o_info = new CViewDataUC();o_info.Info(this.ViewData, id);返回视图();}

Information.aspx(查看)

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="true"CodeBehind="Info.aspx.cs" Inherits="App.Views.Info" %><asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server"><%Html.RenderPartial("~/Views/UserControls/Info.ascx", ViewData["vd_Info"]);%></asp:内容>

Info.ascx(用户控制)

<%foreach (table1 m in (IEnumerable)ViewData.Model){%><div class="left"><br/><br/><p id="中">第 1 栏<br/><%= Html.TextBox("column1", m.column1, new {@class = "textBox", @readonly = "readonly" })%>第 1 栏<br/><%= Html.TextBox("column2", m.column2, new {@class = "textBox", @readonly = "readonly" })%><br/>第 1 栏<br/><%= Html.TextBox("column3", m.column3, new {@class = "textBox", @readonly = "readonly" })%></p>

<%}%>

解决方案

foreach (table1 m in (IEnumerable)ViewData.Model)

m 不是 table1 类型.它是一种匿名类型(select new { ... } in CViewDataUC.cs).

您应该创建一个类来表示您从控制器传递到视图的模型对象的类型.

Im having problems displaying records to my view when passing viewdata to a user control. This is only apparent for linq to sql objects where I am using table joins.

The exception I receive is "Unable to cast object of type '<>f__AnonymousType410[System.String,System.Int32,System.Nullable1[System.DateTime],System.String,System.String,System.String,System.String,System.String,System.Nullable1[System.Single],System.Nullable1[System.Double]]' to type App.Models.table1."

I have searched for a fix to this issue but not too familiar on whats wrong here for me to search for the right subject. This should be working in theory and this works for single table retrieving but when I added a join in their I ran into problems. I am currently using a foreach statement to query through my data via single table declaration. Any help would be greatly appreciated. Thanks in advance.

My current setup is:

CViewDataUC.cs(my class to hold viewdata and data connections specifically for user controls)

public void Info(ViewDataDictionary viewData, int id)
    {
        var dataContext = new testDataContext();

        var info = from table1 in dataContext.table1
                   join table2 in dataContext.table2 on table1.type_id equals table2.type_id
                   join table3 in dataContext.table3 on table1.id equals table3.id
                   join table4 in dataContext.table4 on table1.id equals table4.id
                   where table1.id == id
                   select new
                   {
                       table1.column1,
                       table1.column2,
                       table1.column3,
                       table1.column4,
                       table1.column5,
                       table1.column6,
                       table1.column7,
                       table2.column1,
                       table3.column1,
                       table4.column1
                   };         

        viewData["vd_Info"] = info;

    }

HomeController.cs(Controller)

public ActionResult Information(int id)
        {
            ViewData["Title"] = "Information";


            CViewDataUC o_info = new CViewDataUC();

            o_info.Info(this.ViewData, id);


            return View();
        }

Information.aspx(View)

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="true"
    CodeBehind="Info.aspx.cs" Inherits="App.Views.Info" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <%Html.RenderPartial("~/Views/UserControls/Info.ascx", ViewData["vd_Info"]);%>
</asp:Content>

Info.ascx(User Control)

<%foreach (table1 m in (IEnumerable)ViewData.Model)
  { %>
<div class="left">
    <br />
    <br />
    <p id="medium">
        Column 1
        <br />
        <%= Html.TextBox("column1", m.column1, new {@class = "textBox", @readonly = "readonly" })%>
        Column 1
        <br />
        <%= Html.TextBox("column2", m.column2, new {@class = "textBox", @readonly = "readonly" })%>
        <br />
        Column 1
        <br />
        <%= Html.TextBox("column3", m.column3, new {@class = "textBox", @readonly = "readonly" })%>
                </p>
</div>
<%}%>

解决方案

foreach (table1 m in (IEnumerable)ViewData.Model)

m is not of type table1. It is an anonymous type (select new { ... } in CViewDataUC.cs).

You should create a class that represents the type of the model objects you are passing from controller to view.

这篇关于MVC LINQ to SQL 表连接记录显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆