1

私はこの答えを見つけるために過去1日半を費やしました、そして時々私はとても近くにいるように見えますが、今のところ運がありません。LINQクエリを定義したコントローラーがあり、結果をリストビューに渡す方法を理解しようとしています。以下はコントローラーコードです。

namespace CMS.Controllers
{
    public class SurveyController : Controller
    {

        private SupportEntities supdb = new SupportEntities();
        private BuisnessEntities bsdb = new BuisnessEntities();

        //
        // GET: /Survey/BizSurveyC

        public ViewResult BizSurveyC(string nipaKey, string bOrg)
        {
            // use the next two lines for testing and then either delete or comment them out.
            nipaKey = "22";
            bOrg = "MPC";

            var Cquery = from Mstr in bsdb.BizOrgInsts
                                     join Dat in bsdb.BizSurveyQ on Mstr.ID equals Dat.MASTERID
                                     where Mstr.NIPAKEY == nipaKey & Mstr.FULCIRCORG == bOrg
                                     orderby Mstr.STREETSUFX, Dat.ADDRESS, Mstr.NUMBER
                                     select new { MasterId = Mstr.ID, Name = Mstr.OLDNAME, Mstr.ADDRESS, Mstr.NIPAKEY, Dat.SURVEYDATE, SurveyId = Dat.ID, Dat.RESURVEYOF, Dat.STAMP };

            //ViewBag.BizQuery = Cquery;
            ViewData["BizQuery"] = new SelectList(Cquery);

            return View();
        }

    }
}

見てわかるように、私はViewDataとViewbagを試しましたが、これまでのところ運がありません


現在の状況は次のとおりです。

ViewModelクラス

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CMS.Models
{
    public class BizSurveyCVM
    {
        public long? MasterId { get; set; }
        public string Name { get; set; }
        public string Address { get; set; }
        public string NipaKey { get; set; }
        public string Date { get; set; }
        public long? SurveyId { get; set; }
        public long? Resurvey { get; set; }
        public string DateStamp { get; set; }
    }
}

変更されたアクション

        var Cquery = (from Mstr in bsdb.BizOrgInsts
                                    join Dat in bsdb.BizSurveyQ on Mstr.ID equals Dat.MASTERID
                                    where Mstr.NIPAKEY == nipaKey & Mstr.FULCIRCORG == bOrg
                                    orderby Mstr.STREETSUFX, Dat.ADDRESS, Mstr.NUMBER
                                    select new BizSurveyCVM
                                    {
                                        MasterId = Mstr.ID,
                                        Name = Mstr.OLDNAME,
                                        Address = Mstr.ADDRESS,
                                        NipaKey = Mstr.NIPAKEY,
                                        Date = Dat.SURVEYDATE,
                                        SurveyId = Dat.ID,
                                        Resurvey = Dat.RESURVEYOF,
                                        DateStamp = Dat.STAMP
                                    }).ToList();

        return View(Cquery);
    }

BizSurveyCビュー

@model List<CMS.Models.BizSurveyCVM>

<table>
    <tr>
        <th>
            MasterId
        </th>
        <th>
            Name
        </th>
        <th>
            Address
        </th>
        <th>
            NipaKey
        </th>
        <th>
            Date
        </th>
        <th>
            SurveyId
        </th>
        <th>
            Resurvey
        </th>
        <th>
            DateStamp
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.MasterId)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Address)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.NipaKey)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Date)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.SurveyId)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Resurvey)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.DateStamp)
        </td>
</table>

結果のビューは次のとおりです。

申し訳ありませんが、画像を保存することはまだ許可されていませんが、ビューにはヘッダーがありますが、データはありません。

私は明らかにビューまたはおそらくクエリでやるべきことがいくつかありますが、物事ははるかに良く見えているので、みんなの助けを借りてください。どうもありがとうございます

4

3 に答える 3

2

クエリ結果が何であれ保持する ViewModel クラスを作成し、新しい ViewModel クラスのビューを厳密に型指定することができます。

ViewModel クラス:

public class BizSurveyCVM 
{
    public long MasterId { get; set; }
    public string Name { get; set; }
    ...
}

変更されたアクション:

var Cquery = (from Mstr in bsdb.BizOrgInsts
              join Dat in bsdb.BizSurveyQ on Mstr.ID equals Dat.MASTERID
              where Mstr.NIPAKEY == nipaKey & Mstr.FULCIRCORG == bOrg
              orderby Mstr.STREETSUFX, Dat.ADDRESS, Mstr.NUMBER
              select new BizSurveyCVM  { MasterId = Mstr.ID, Name = Mstr.OLDNAME, ...}
             ).ToList();

return View(Cquery);

BizSurveyC ビュー

@model List<{namespace}.BizSurveyCVM>

@foreach(var item in Model) {
     {HTML Mark-up}
}

編集:更新された質問に基づくビューの更新された例を次に示します。

@model List<{namespace}.BizSurveyCVM>

@foreach(var item in Model) {
    <tr>
        <td>
            @item.MasterId
        </td>
        <td>
            @item.Name
        </td>
        <td>
            @item.Address
        </td>
        ...
    </tr>
}
于 2012-05-31T14:42:27.753 に答える
0
return View(Cquery);

また

return View("ViewName",Cquery);

ビューでは、モデル タイプは Cquery のタイプと一致する必要があります。しかし、(VS を使用していると仮定して) メソッド本体のどこかを右クリックし、[ビューの追加] をクリックして、モデル タイプのリストからモデルを選択する方が簡単だと思います。

于 2012-05-31T14:09:15.653 に答える
0

本当にやりたいですか?

ビュー内のコレクションの方が便利なので、選択リストなど、必要なものを何でも作成できます。

次のように、選択リストでデータ値と表示値が誰であるかを定義することを忘れないでください..

ViewData["BizQuery"] = new SelectList(Cquery, null, "MasterId", "Name");

わかりました、あなたのコードは私のために働くので、ビューをチェックしてください。ビューデータでオブジェクトの正しいキャストを行う必要があります

このようなものが必要です

@foreach (SelectListItem item in (SelectList)ViewData["BizQuery"])
{

    <p>@item.Text</p>

}
于 2012-05-31T14:42:05.653 に答える