我创建了View模型,用于组合两个类以在单个视图中显示多个模型。我创建了linq查询以在View中显示数据。这是我的控制器。
public ActionResult Details(int id = 0)
{
var sp_details = (from s in db.service_provider
join p in db.pictures on s.SPID equals p.SPID
join c in db.cities on s.City_ID equals c.City_ID
//where s.SPID==id
select new ImageData()
{
SPID = s.SPID,
Sp_name = s.Sp_name,
Sp_location = s.Sp_location,
Sp_description = s.Sp_description,
Sp_rate = s.Sp_rate,
service_type = s.service_type,
Cityname= c.Cityname,
pic = p.pic });
if (sp_details == null)
{
return HttpNotFound();
}
return View(sp_details);
}
这是详情视图。我想获得一个特定的配置文件来查看。
@model IEnumerable<WEB1.ViewModel.ImageData>
@{
ViewBag.Title = "Details";
}
<div class="span9">
@foreach (var item in Model)
{
<p class="name">@Html.DisplayFor(modelItem => item.Sp_name)</p>
<p class="cattype">@Html.DisplayFor(modelItem => item.Sp_location)</p>
<table>
<tr>
<td>Opening Hours</td>
<td> @Html.DisplayFor(modelItem => item.Cnt_wh)</td>
</tr>
<tr>
<td></td>
<td>@Html.DisplayFor(modelItem => item.Sp_description)</td>
</tr>
<tr>
<td>Rate</td>
<td>@Html.DisplayFor(modelItem => item.Sp_rate)</td>
</tr>
<tr>
<td></td>
<td><p>@Html.ActionLink("Back to List", "Hotel")</p></td>
</tr>
</table>}
这是索引视图。它在点击超链接后传递了id值。
@foreach (var item in Model)
{
<div class="item1">
<img src="data:image/png;base64,@Convert.ToBase64String(item.pic,0,item.pic.Length)" width="100" />
<p class="name">@Html.ActionLink(item.Sp_name, "Details", new { id = item.SPID },null)</p>
<p class="cattype">@Html.DisplayFor(modelItem => item.Sp_location)</p>
<p>@Html.DisplayFor(modelItem => item.Cityname)</p>
<p>@Html.DisplayFor(modelItem => item.Sp_rate)</p>
</div>
}
我在查询中删除了注释行,然后我获得了所有列表项。但是我想获得一个特定的值。我添加了注释行,然后我得到了空白页面,没有错误但没有数据。