【问题标题】:Show List string in HTML(MVC) using loop使用循环在 HTML(MVC) 中显示列表字符串
【发布时间】:2013-10-08 09:51:57
【问题描述】:

我正在尝试使用循环在我的 HTML 中显示 List<string>。我得到以下输出:

System.Collections.Generic.List`1[System.String]

我以前见过这个,但不是我目前编码的方式: 我尝试了 2 个循环来显示 List<string> 中的值,但它们都返回相同的上述结果:

HTML

@{
    Layout = "~/Administration/Views/Shared/_AdminLayout.cshtml";
}
@using Telerik.Web.Mvc.UI
@using Nop.Plugin.Misc.ExportAttributes.Models;
@model ImportCalculationSheetModel
@using Nop.Web.Framework;
@using (Html.BeginForm())
{
   <ul>
      @for (int i = 0; i < Model.FailedProductIdsList.Count; i++)
      {
          <li><b>@Convert.ToString(Model.FailedProductIdsList[i])</b></li>
      }
   </ul>
}

For循环:

<ul>
   @for (int i = 0; i < Model.FailedProductIdsList.Count; i++)
   {
      <li><b>@Convert.ToString(Model.FailedProductIdsList[i])</b></li>
   }
</ul>

Foreach 循环:

<ul>
   @foreach(var productId in Model.FailedProductIdsList)
   {
      <li><b>@Convert.ToString(productId);</b></li>
   }
</ul>

调试时我看到List&lt;string&gt;中只插入了一个值:"99999" 所以它甚至不需要转换为字符串,但我这样做只是为了确定,因为不转换时结果是一样的。

我正在使用的模型:ImportCalculationSheetModelView 中使用 @model ImportCalculationSheetModel 引用

型号

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Nop.Web.Framework;
using Nop.Web.Framework.Mvc;

namespace Nop.Plugin.Misc.ExportAttributes.Models
{
    public class ImportCalculationSheetModel : BaseNopModel
    {
        public List<string> FailedProductIdsList { get; set; }
    }
}

所以我的模型只填充了 1 个List&lt;string&gt;,列表中填充了我的Controller

控制器

[HttpGet]
public ActionResult ImportCalculationSheetSucceededWithFailures(ImportCalculationSheetModel model)
{
   return View("Nop.Plugin.Misc.ExportAttributes.Views.MiscExportAttributes.ImportCalculationSheetSucceededWithFailures", model);
}

[HttpPost]
public ActionResult ImportCalculationSheet(ImportCalculationSheetModel model)
{
    if (ModelState.IsValid)
    {
           ImportFromCalculationSheet _importFromCalculationSheet = new      ImportFromCalculationSheet();
           var file = Request.Files["importexcelfile"];
           if (file != null && file.ContentLength > 0)
           {
              _importFromCalculationSheet.ImportProductsFromXlsx(file.InputStream);
              model.FailedProductIdsList = _importFromCalculationSheet.FailedToUpdateIds;
           }
           else
           {
             return RedirectToAction("ImportCalculationSheet", model);
           }
           if (model.FailedProductIdsList.Count > 0)
           {
             return RedirectToAction("ImportCalculationSheetSucceededWithFailures", model);
           }
           else
           {
             return RedirectToAction("ImportCalculationSheetSucceeded", model);
           }
   }
   else
   {
       return RedirectToAction("ImportCalculationSheet", model);
   }
}

FailedProductIdsList 在以下行填充:

model.FailedProductIdsList = _importFromCalculationSheet.FailedToUpdateIds;

我可以看到它在调试时不会失败。

问题

for循环和foreach循环为什么会返回:

System.Collections.Generic.List`1[System.String]

当我一次只抓取List&lt;string&gt; 中的一项甚至将其转换为字符串时?有没有办法在视图中更好地调试这个问题?(视图中的断点没有命中)

【问题讨论】:

  • FailedProductIdsList 已经是一个字符串列表,因此无需在循环内再次将其转换为字符串。
  • 看到这个 - stackoverflow.com/questions/17469697/… 。看来可能是 `Convert.ToString()` 导致了所有问题。
  • 您说“model.FailedProductIdsList = _importFromCalculationSheet.FailedToUpdateIds;”中没有错误我怀疑这是问题所在。但是,您能否检查一下分配给列表的实际类型。也尝试做 _importFromCalculationSheet.FailedToUpdateIds.ToList();
  • @Raj 我已经检查过了,但它给了我相同的结果。
  • 你说断点没有命中视图页面。也许您正在引用该页面的旧版本?我建议关闭 VS 并删除所有 ASP.NET 临时文件并重新启动应用程序。如果这不能解决问题,请尝试在示例 MVC 应用程序中复制您的问题。确保您具有相同的设置,除了任何其他附加依赖项。祝你好运。

标签: c# html asp.net-mvc razor


【解决方案1】:

使用 foreach:

无需转换为字符串,因为FailedProductIdsList 已经是字符串列表。

<ul>
   @foreach(var productId in Model.FailedProductIdsList)
   {
      <li><b>@productId</b></li>
   }
</ul>

编辑:

将控制器简化为:

[HttpPost]
public ActionResult ImportCalculationSheet(ImportCalculationSheetModel model)
{
    var m = new ImportCalculationSheetModel { 
        FailedProductIdsList = new List<string> { "test1", "test2", "test3" } 
    };

    if (ModelState.IsValid)
    {
        return View("ViewName", m);
    }
}

这会打印列表吗?

【讨论】:

  • 这是我在问题中展示的那种 foreach 循环,我已经尝试过了,有无转换为字符串
  • 那么列表是空的。有什么问题?
  • 也许模型没有正确发送到视图,但列表已设置并且第一个值已按问题所述填充。所以它至少应该打印出问题中提到的“99999”。
  • 使用你提供给我的控制器返回相同的结果:System.Collections.Generic.List`1[System.String]
【解决方案2】:

我发现并解决了问题:

我正在重定向到我的代码中的一个动作,这是我不应该做的,因为那时模型将被清除。

所以不要使用以下代码:

if (model.FailedProductIdsList.Count > 0)
{
   return RedirectToAction("ImportCalculationSheetSucceededWithFailures", model);
}

我现在使用以下代码:

if (model.FailedProductIdsList.Count > 0)
{
   return View("Nop.Plugin.Misc.ExportAttributes.Views.MiscExportAttributes.ImportCalculationSheetSucceededWithFailures", model);
}

不同之处在于我没有调用其他方法来创建新的干净模型,而是通过这种方式将模型直接发送到视图。

【讨论】:

  • 太好了,您找到了答案。您说“不同之处在于我没有调用创建新的干净模型的其他方法”这意味着您的以下获取操作? [HttpGet] public ActionResult ImportCalculationSheetSucceededWithFailures(ImportCalculationSheetModel model) { 在将其发送到视图之前,您是否在此处创建了一个新的干净模型?还是你在说别的?
  • RedirectToAction,确实将我重定向到创建新模型的 [HttpGet] 方法,因此列表始终为空。我现在将填充模型发送到视图,而不创建新模型,如上面的代码所示。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-18
  • 1970-01-01
  • 2021-04-02
  • 2013-11-27
相关资源
最近更新 更多