문제

viewData를 사용자 컨트롤로 전달할 때 내보기에 레코드를 표시하는 데 문제가 있습니다. 이것은 테이블 조인을 사용하는 LINQ에서 SQL 객체에만 분명합니다.

내가받는 예외는 "유형의 객체를 캐스트 할 수 없다 '<> f__anonymoustype4입니다.10[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을 입력하려면. "

이 문제에 대한 수정 사항을 검색했지만 올바른 주제를 검색하기 위해 여기에 무엇이 잘못되었는지 너무 익숙하지 않습니다. 이것은 이론적으로 작동해야하며 이것은 단일 테이블 검색에 효과가 있지만, INS IN IN IN BROSTION에 가입을 추가했을 때 문제가 발생했습니다. 현재 단일 테이블 선언을 통해 내 데이터를 쿼리하기 위해 Foreach 문을 사용하고 있습니다. 모든 도움은 대단히 감사하겠습니다. 미리 감사드립니다.

내 현재 설정은 다음과 같습니다.

cviewDatauc.cs (사용자 컨트롤을 위해 특별히 ViewData 및 데이터 연결을 보유하는 내 클래스)

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>
<%}%>
도움이 되었습니까?

해결책

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

m 유형이 아닙니다 table1. 익명 유형입니다 (select new { ... } cviewdatauc.cs).

컨트롤러에서 볼 수있는 모델 객체의 유형을 나타내는 클래스를 만들어야합니다.

다른 팁

감사합니다. 매력처럼 작동했습니다. 나는 문제를 완전히 간과했는데, 월요일 인 것 같아요 : p. 그러나 같은 문제를 겪고 있으며 여전히 MVC 및 LINQ에 새롭고 SQL을 처음 접하는 사람들의 경우 문제는 다음 단계를 쉽게 해결합니다.

  1. cinformation.cs query에 비슷한 변수를 추가하십시오.

    공개 수업 cinformation {

    public CInformation() { }
    
    public string _column1{ get; set; }
    public string _column2{ get; set; }
    ...
    

    }

  2. 쿼리에서

    public void uc_vdgeninfodc (ViewDatadictionary ViewData, int id) {

        var dataContext = new testDataContext();
    
        IQueryable<CInformation> 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.test_id == id
                   select new CInformation
                   {
                       _column1 = table1.column1.ToString(),
                       _column2 = table2.column1.ToString(),
                       ...
                   };         
    
        viewData["vd_Info"] = info;
    
    }
    
  3. 사용자 컨트롤 또는보기에서

    foreach (cinformation m in (ienumerable) ViewData.Model) {

     m.column1
     m.column2
     ...
    

    }

고마워요, 이것은 나에게 많은 도움이되었습니다!

한 가지 작은 의견은 각도 브래킷을 사용해야합니다.

IQueryable<CInformation> info = from table1 in dataContext.table1
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top