我已经为我的项目使用了db first方法。我创建了SQL server db并创建了ADO.net模型。在数据模型中,有几个表即service_provider,picture,comment。我在单个视图中为显示数据创建了viewmodel。下面我给出了我的Controller方法,用于检索特定的配置文件。
public ActionResult Details(int id = 0)
{
List<ImageData> details = new List<ImageData>();
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
select new { s.SPID,s.Sp_name, s.Sp_location,s.Sp_description, s.Sp_rate, s.service_type, c.Cityname, p.pic });
foreach (var item in sp_details)
{
ImageData SpView = new ImageData(); // ViewModel
SpView.Sp_name = item.Sp_name;
SpView.Sp_location = item.Sp_location;
SpView.Cityname = item.Cityname;
SpView.Sp_rate = item.Sp_rate;
SpView.pic = item.pic;
if (SpView.SPID == id)
{
details.Add(SpView);
}
}
if (details == null)
{
return HttpNotFound();
}
return View(details);
}
这是我的观点
@model List<WEB1.ViewModel.ImageData
@{
ViewBag.Title = "Details";
}
<div class="span9">
<div class="itembody">
<div class="itemprofile">
@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>Place Type</td>
<td>@Html.DisplayFor(modelItem => item.Cnt_type)</td>
</tr>
<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", "Index")</p></td>
</tr>
</table>
}
</div>
</div>
</div>
上面的代码显示列表中的所有数据。但我想只显示一个 SPID == id 的配置文件。有人可以帮我解决我的问题。
答案 0 :(得分:0)
这是你需要做的。过滤您的选择,然后它将返回您要显示的项目。
public ActionResult Details(int id = 0)
{
List<ImageData> details = new List<ImageData>();
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 { s.SPID,s.Sp_name, s.Sp_location,s.Sp_description, s.Sp_rate, s.service_type, c.Cityname, p.pic });