1

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

2
  • 1
    @model IEnumerable<string> Commented Sep 22, 2016 at 7:37
  • thanks haim770, and can you please let me know what is the role of the @model IEnumerable<string> is doing here? Commented Sep 22, 2016 at 7:43

3 Answers 3

3

Use @model string[] or @model IEnumerable instead of @model List.

Because txtFiles is not type. Hope this will help you.

Sign up to request clarification or add additional context in comments.

Comments

2

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>

2 Comments

Thanks Raphaël but can you please let me know what exactly the @model IEnumerable<string> is doing here?
In @model, you should have the TYPE of your model. txtFiles, which is what you send to view as model, is a string[] (this is what Directory.GetFiles returns) . And arrays implement IEnumerable, so you can use IEnumerable<string> also.
2

Instead of using List<txtFiles> please use string[] or IEnumerable<string> in Razor code. You tried to use txtFiles as a type, which is wrong becasuse it's name of variable.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.