基于 ASP.NET MVC 中的查询将数据打印到屏幕?

发布于 2024-08-06 04:47:08 字数 1032 浏览 5 评论 0原文

我正在开发一个应用程序,我想循环遍历一些数据,其中数据中的标志设置为 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">&nbsp;</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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

—━☆沉默づ 2024-08-13 04:47:08

我不确定这是个好主意。您的视图应该尽可能“轻”,如果您开始在其中放入 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.

风苍溪 2024-08-13 04:47:08

您的视图根本不应该包含查询。您的模型应该已经填充了需要显示的内容。您原来的 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();

書生途 2024-08-13 04:47:08

您可以添加一个 where 子句:

<% For Each Ticket As hdCall in ViewData.Model.Where(m => m.IsUpcoming==true) %>

但在我看来,这属于模型(或控制器)。

You could add a where clause:

<% For Each Ticket As hdCall in ViewData.Model.Where(m => m.IsUpcoming==true) %>

But in my opinion this belongs in the Model (or the controller).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文