Suffii Suffii - 2 months ago 9
ASP.NET (C#) Question

Having Issue On Passing Array to View From Controller in Asp.Net MVC

I have a Controller action

ActionResult
which is listing all files in directory as:

public ActionResult Index()
{
string[] txtFiles = Directory.GetFiles(Server.MapPath("~/TextFiles/"));
return View(txtFiles);
}


and in
index.cshtml
I have

@model List<txtFiles>
@{
ViewBag.Title = "Index";
}

<h2>Index</h2>

<ul>
@foreach (var i in Model)
{
<li>@Html.ActionLink(System.IO.Path.GetFileName(i), "FileContent")</li>
}
</ul>


but I am not sure what should I pass in
model
? I already tried
@model List<txtFiles>
but it didn't work and I am getting this error

enter image description here

Answer

You model is not a List<txtFiles> but a string[]

Change the model in your view.

@model List<txtFiles> should become @model string[] or @model IEnumerable<string>