基于 ASP.NET MVC 中的查询将数据打印到屏幕?
我正在开发一个应用程序,我想循环遍历一些数据,其中数据中的标志设置为 1 或 2 并将其打印到屏幕上。
我正在使用的想法来自 MVC 教程之一:
<ul>
<% For Each m As Movie In ViewData.Model%>
<li><%= m.Title %></li>
<% Next%>
</ul>
但我想做与上面类似的事情,但使用 LINQ 语句来获取所需的数据。
我该怎么做?
我已经开始对上述代码进行类似的复制,我知道这不会起作用,但我只是看看它对我的数据做了什么。
<table cellpadding="0" cellspacing="0" width="643">
<% For Each Ticket As hdCall In ViewData.Model%>
<tr>
<td width="54" class="ticketList"> </td>
<td width="182" class="ticketList"><%=Ticket.Title%></td>
<td width="88" class="ticketList"></td>
<td width="148" class="ticketList"></td>
<td width="58" class="ticketList"></td>
<td width="115" class="ticketList"></td>
</tr>
<% Next%>
</table>
I have an application I'm working on where I want to loop through some data where a flag in the data is set to 1 or 2 and print it to screen.
The idea I'm working on using is from one of the MVC tutorials:
<ul>
<% For Each m As Movie In ViewData.Model%>
<li><%= m.Title %></li>
<% Next%>
</ul>
But I want to do similar to the above but using a LINQ statement to grab the required data.
How do I do that?
I've started out with a like-for-like copy more or less of the above code, which I know won't do the trick but I was just seeing what it did with my data.
<table cellpadding="0" cellspacing="0" width="643">
<% For Each Ticket As hdCall In ViewData.Model%>
<tr>
<td width="54" class="ticketList"> </td>
<td width="182" class="ticketList"><%=Ticket.Title%></td>
<td width="88" class="ticketList"></td>
<td width="148" class="ticketList"></td>
<td width="58" class="ticketList"></td>
<td width="115" class="ticketList"></td>
</tr>
<% Next%>
</table>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不确定这是个好主意。您的视图应该尽可能“轻”,如果您开始在其中放入 Linq 查询,它就会开始变得相当头重脚轻。
更好的方法是使用从存储库对象检索所述数据的 Linq 查询在控制器方法中提取数据。查看 NerdDinner 教程以获取有关此内容的更多信息。
无论如何,正如 Jao 指出的那样,您可以将 IQueryable 传递给视图,并且它的迭代看起来与 IEnumerable 的迭代基本相同。因此,如果您所做的只是枚举集合,那么这看起来非常干净。
I'm not sure this is a good idea. Your view should be as "light" as possible, and it would start to get pretty top-heavy if you started putting Linq queries in it.
A better approach is to pull data in your controller method, using a Linq query that retrieves said data from a repository object. Check out the NerdDinner tutorials for more information about this.
Anyway, as Jao points out, you can pass an IQueryable to the view, and the iteration over it looks basically the same as that of an IEnumerable. So if all you're doing is enumerating over the collection, this looks pretty clean.
您的视图根本不应该包含查询。您的模型应该已经填充了需要显示的内容。您原来的
foreach
是正确的。通常,我从控制器调用数据访问层中的查询,并使用结果填充模型。例如:this.Model = _ticketRepository.FindAllUpcoming();
返回视图();
Your view should not contain the query at all. Your model should already be populated with what it needs to show. Your original
foreach
was correct. Normally, I call a query in my data access layer from my controller and populate the model with the results. For example:this.Model = _ticketRepository.FindAllUpcoming();
return View();
您可以添加一个 where 子句:
但在我看来,这属于模型(或控制器)。
You could add a where clause:
But in my opinion this belongs in the Model (or the controller).