HomeController.cs
[HttpPost]
public ActionResult Index(int searchtext)
{
var data = (from pm in db.ProductMasters
join shi in db.SuppliersHotelsInfoes
on pm.ProductID equals shi.LocHotelID
where shi.SearchID == searchtext
select new {pm.ProductId,pm.Image,shi.HotelName,shi.HotelPrice}).ToList().Take(10);
ViewBag.Data = data;
return View();
}将数据传输到视图
<% try { %>
<tr><td>
<% foreach(var item in ViewBag.Data){ %>
<img alt="" src="<%= item. %>" />
</td><%} %> <% foreach(var item in ViewBag.Data) { %>
<td><%: Html.DisplayFor(modelItem => "")%></td>
<td><%: Html.DisplayFor(modelItem => "")%> </td>
<td><%: Html.DisplayFor(modelItem => "")%></td>
</tr>
<%} }%>
<%catch(Exception ex){ }%>
这里,如何获取"var item“中的值
发布于 2014-04-17 20:53:38
你真的应该用ViewModel,我不记得上次用ViewBag是什么时候了
我还将在这里使用Razor,它的语法要简洁得多。如果您只是呈现数据,则不需要使用DisplayFor:
@foreach(var item in ViewBag.Data){
<tr>
<td><img alt="" src="@item.Image" /><td>
<td>@item.ProductId</td>
<td>@item.HotelName</td>
<td>@item.HotelPrice</td>
</tr>
}https://stackoverflow.com/questions/23133416
复制相似问题