I am trying to show a List<string> in my HTML using a loop. I get the following output:
System.Collections.Generic.List`1[System.String]
I have seen this before, but not in the way I am currently coding:
I tried 2 loops for showing the values in that List<string> but both of them return the same above mentioned result:
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 loop:
<ul>
@for (int i = 0; i < Model.FailedProductIdsList.Count; i++)
{
<li><b>@Convert.ToString(Model.FailedProductIdsList[i])</b></li>
}
</ul>
Foreach loop:
<ul>
@foreach(var productId in Model.FailedProductIdsList)
{
<li><b>@Convert.ToString(productId);</b></li>
}
</ul>
When debugging I see that only one value is inserted in the List<string>: "99999"
So it isn't even necessary converting to string, but I did it just to be sure, since the result is the same when not converting.
The model I am using: ImportCalculationSheetModel is referenced in View using @model ImportCalculationSheetModel
Model
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; }
}
}
So my Model is only filled with 1 List<string>, the list gets filled in my Controller.
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 gets filled at following line:
model.FailedProductIdsList = _importFromCalculationSheet.FailedToUpdateIds;
I can see that it doesn't fail there, when debugging.
Question
Why are the for loop and foreach loop returning:
System.Collections.Generic.List`1[System.String]
When I only grab one item in the List<string> at a time and even convert it to a string? Is there a way to debug this problem better in the view?(Breakpoints in the View aren't hit)
FailedProductIdsListis already a list of strings so no need to convert it to a string again inside your loops.