これは、あなたが既に選択Value
しValue
ており、 のようなプロパティがないためです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>