在foreach循环中的MVC if语句无法正常工作 [英] MVC if statement inside a foreach loop not working correctly

查看:109
本文介绍了在foreach循环中的MVC if语句无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 @ {var list = Model.ToList(); 
for(int i = 0; i< list.Count; i ++)
{
< tr>
< td>
@ViewData [" CurrentFirstName"] @ViewData [" CurrentLastName"]
< / td>
< td>
@if(list [i] .InvoiceCount> 0){
@ Html.DisplayFor(modelItem => list [i] .InvoiceCount)}
else
{@ Html.Raw("< text> No Invoices found。< / text>")}
< / td>
< td>
@ Html.DisplayFor(modelItem => list [i] .TotalInvoicesSum)
< / td>
< / tr>

我有一个学校的MVC项目,我完成了作业。


但是,我试图在每个循环中找到我的if语句中的错误。


查询返回带有客户名称的结果,他们的发票数量和发票销售总额(如果有的话),如果找到的话。


以下代码工作得很好,除非发票数量= 0,我想要插入"未找到发票"当计数为零时。


目前,当发票计数= 0时,我的视图只显示一个空行。


发票计数> 0:



发票计数= 0:



提前致谢。

解决方案

Hi Pete,


看起来没有发票时,模型中没有创建行。如果这是来自带有JOIN的SQL查询(可能是它),则需要将JOIN更改为OUTER JOIN,即使有
没有子行,也会包含父行。



试试并告诉我们。



谢谢,


@{ var list = Model.ToList();
   for (int i = 0; i < list.Count; i++)
   {
   <tr>
     <td>
@ViewData["CurrentFirstName"] @ViewData["CurrentLastName"]
     </td>
     <td>
     @if (list[i].InvoiceCount > 0) {  
@Html.DisplayFor(modelItem => list[i].InvoiceCount) }
else
{ @Html.Raw("<text>No invoices found.</text>") }
     </td>
     <td>
@Html.DisplayFor(modelItem => list[i].TotalInvoicesSum)
     </td>
   </tr>

Hi, I have an MVC project for school, I have the assignment completed.

However, I was trying to find what is wrong in my if statement inside my for each loop.

A query returns the results with a customers name, their invoice count and the total sales amount of invoices sales, if any, that are found.

The following code works just fine, except if the invoice count=0, I want to insert "No invoices found" when the count is zero.

Currently, my view just displays an empty row when invoice count = 0.

Invoice count > 0:

Invoice count = 0:

Thanks in advance.

解决方案

Hi Pete,

It looks like when there are no invoices the row is not being created in the model. If this is coming from a SQL query with a JOIN , which it probably is, you need to change the JOIN to be an OUTER JOIN, which will include the parent row even if there are no children rows.

Try that and let us know.

Thanks,


这篇关于在foreach循环中的MVC if语句无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆