将视图数据传递给用户控件时,我无法在视图中显示记录。这仅对我使用表连接的 linq to sql 对象很明显。
我收到的异常是“无法转换类型为 '<>f__AnonymousType4 10[System.String,System.Int32,System.Nullable
1[System.DateTime],System.String,System.String,System.String,System.String,System.String,System.Nullable 1[System.Single],System.Nullable
1[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
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(控制器)
public ActionResult Information(int id)
{
ViewData["Title"] = "Information";
CViewDataUC o_info = new CViewDataUC();
o_info.Info(this.ViewData, id);
return View();
}
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:Content>
Info.ascx(用户控制)
<%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>
<%}%>