我花了一天半的时间试图找到这个答案,有时似乎我如此接近,但到目前为止还没有运气。 我有一个控制器,我在其中定义了LINQ查询,并试图找出如何将结果传递到列表视图。以下是Controller代码:
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>
这是结果视图:
抱歉不允许保存图像,但我认为我没有数据。
我显然在视图或者查询中有一些工作要做,但事情看起来要好得多,谢谢大家的帮助。非常感谢你
答案 0 :(得分: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>
}
答案 1 :(得分:0)
return View(Cquery);
或
return View("ViewName",Cquery);
然后在您的视图中,模型类型应与Cquery的类型匹配。但我发现更容易(假设你正在使用VS)只需在方法体中右键单击并单击“添加视图”,然后从模型类型列表中选择模型。
答案 2 :(得分:0)
你确定要做吗?
它在视图中的集合更有用,因此您可以构建所需的任何内容,例如选择列表。
不要忘记在选择列表中定义谁是您的数据值和显示值,例如..
ViewData["BizQuery"] = new SelectList(Cquery, null, "MasterId", "Name");
好的,您的代码适合我,所以检查您的视图,您必须在viewdata中对对象进行正确的转换
你需要这样的东西
@foreach (SelectListItem item in (SelectList)ViewData["BizQuery"])
{
<p>@item.Text</p>
}