7

コントローラーからビューにデータを渡そうとしています。ウェブを検索しましたが、解決策が見つかりませんでした。

私がこれを行うと、動作します:

コントローラ:

var yyy = (from a in Connection.Db.Authorities select a) ;
ViewBag.data = yyy;

意見:

@foreach(var item in ViewBag.data)
{
    @item.Value
}

ただし、次のコードは機能しません。

コントローラ:

var yyy = (from a in Connection.Db.Authorities select new {Value = a.Value, TypeCode = a.TypeCode, Return = Calculate(a.Return)}) ;
ViewBag.data = yyy;

意見:

@foreach(var item in ViewBag.data)
{
    @item.Value
}

ビューファイルに「アイテムに値の定義が含まれていません」と表示されます。

どんな助けでも素晴らしいでしょう。

ありがとうございました。

-編集済み: 2 番目のコントローラー linq クエリを更新しました。最初のコントローラー linq クエリを修正しました。

4

2 に答える 2

5

これは、あなたが既に選択ValueValueており、 のようなプロパティがないためですValue。コントローラーで変更する必要があります:

var yyy = (from a in Connection.Db.Authorities select a.Value);

var yyy = (from a in Connection.Db.Authorities select a);

または、ビューを次のように変更します

@foreach(var item in ViewBag.data)
{
    @item
}

//////////////////////////////////////////////編集/ /////////////////////////////////////////////
あなたがすべきより匿名オブジェクトを使用しないでください。を作成する必要がありますViewModelClass。例えば:

public class AuthoritiesViewModel
        {
            public string Value { get; set; }
            public string TypeCode { get; set; }
            public string Return { get; set; }
        }

コントローラーを変更します。

var yyy = (from a in Connection.Db.Authorities select new AuthoritiesViewModel{ Value = a.Value, TypeCode = a.TypeCode, Return = Calculate(a.Return)});
ViewBag.data = yyy;

あなたの見解では、以下を使用することができます:

<table>
    <tr>
         <th>Value</th>
         <th>TypeCode</th>
         <th>Return</th>
    </tr>
@foreach(AuthoritiesViewModel item in ViewBag.data)
{
    <tr>
         <td>@item.Value<td>
         <td>@item.TypeCode<td>
         <td>@item.Return<td>
    </tr>
}
</table>

また、あなたに質問があります。ViewBagコントローラーからビューにデータを渡すために使用するのはなぜですか? Model を使用してこれらのデータを MVC パターンに従ってビューに渡してみませんか?

////////////////////////////////////////////// その他の編集//////////////////////////////////////////////
送信する複数のクエリ結果 より複雑なモデルを作成できます。例えば:

public class AuthoritiesViewModel
        {
            public string Value { get; set; }
            public string TypeCode { get; set; }
            public string Return { get; set; }
        }

        public class AnotherQueryViewModel
        {
            public string AnotherQueryValue { get; set; }
            public string AnotherQueryTypeCode { get; set; }
            public string AnotherQueryReturn { get; set; }
        }

        public class ModelClass
        {
            IEnumerable<AuthoritiesViewModel> Authorities { get; set; }
            IEnumerable<AnotherQueryViewModel> AnotherQueryResults { get; set; }
        }

そしてコントローラーを変更します:

var yyy = (from a in Connection.Db.Authorities select new AuthoritiesViewModel{ Value = a.Value, TypeCode = a.TypeCode, Return = Calculate(a.Return)});

// do your another select
var zzz = (from smthing select new AnotherQueryViewModel ...)

// create model instance
ModelClass model = new ModelClass() 
{
    Authorities = yyy.AsEnumerable(),
    AnotherQueryResults = zzz..AsEnumerable()
}

// return view with model
return View("view", model);

ビューでは、次を使用できます。

@model ModelClass

@*display first query result*@
<table>
    <tr>
         <th>Value</th>
         <th>TypeCode</th>
         <th>Return</th>
    </tr>
@foreach(AuthoritiesViewModel item in Model.Authorities)
{
    <tr>
         <td>@item.Value<td>
         <td>@item.TypeCode<td>
         <td>@item.Return<td>
    </tr>
}
</table>

@*display second query result*@
<table>
    <tr>
         <th>Another Query Value</th>
         <th>Another Query TypeCode</th>
         <th>Another Query Return</th>
    </tr>
@foreach(AnotherQueryViewModel item in Model.AnotherQueryResults)
{
    <tr>
         <td>@item.AnotherQueryValue<td>
         <td>@item.AnotherQueryTypeCode<td>
         <td>@item.AnotherQueryReturn<td>
    </tr>
}
</table>
于 2013-05-28T13:51:41.027 に答える
0

このようにsthを使用します

ViewBag.qualification = new SelectList(db.Lookups.Where(x => x.lookup_type == "条件"), "lookup_content", "lookup_content");

于 2016-10-14T10:36:09.353 に答える