sustekj sustekj - 17 days ago 4x
C# Question

Table value from View to Controller ( ASP.NET MVC )

I am working on a project in ASP.NET MVC 5, I have a TABLE in my VIEW, where I am showing the content of an ftp folder via list and Viewbag.

I need the Table to be clickable. I used *.js file so the rows are clickable, but now I have a problem with getting the name of the file (value of a row) back to Controller.

View code :

<table class="row col-md-12 leva table-responsive table-bordered table-hover ">
<tbody data-link="row" class="rowlink">
@foreach (var item in ViewBag.files)
<td class=" row col-md-12 "><a href="#" target="_blank"> <h5> @item</h5></a></td>


And Controller code:

List<string> ftpfiles = new List<string>();
var ftpco = new Project.Helpers.ftp(@path, name, passwd);
string[] simpleDirectoryListing = ftpco.directoryListSimple("");

for (int i = 0; i < simpleDirectoryListing.Count(); i++)
if (simpleDirectoryListing[i].Length >= 3)

ViewBag.files = soubory;

catch { ViewBag.files = "nenacteno"; }

Ben Ben

It is not clear what the interaction should be when the user clicks on a row. Do you want:

  1. The Javascript to download the file and show a preview or contents
  2. Allow the user to edit the file
  3. Get metadata about the file
  4. The user to be directed to another action method which downloads the file

I'm going to lay out the stubbing for a file download, but you can change it to whatever interaction you want by changing the action method logic. What you should do is pass the file name through GET in a URL (Since this is the MVC way). The view would be (the formatting is missing the @'s because the code formatter is not having it for me at the moment):

    foreach (var item in ViewBag.files)
            <td>@Html.ActionLink(item, "Download", "YourController", new { id = item }, null)

and then you would create an action in your controller that had a get parameter:

//GET: /YourController/Download/{id}
public ActionResult Download(string id)
    //download logic

You pass the ftp doc name via GET to the action method by adding the new { id = item } to the html helper. You would use the string called 'id' in this case in your logic to retrieve the document, or if your original intent was NOT to download the doc, you could implement whatever you wanted in a similar way.

By the way, make sure to use the correct overload of @Html.ActionLink - if you notice I put 4 parameters, null being the last. In MVC5 they changed this and if you don't put the 4th parameter you will get /controller/action?id=Value rather than /controller/action/value in the URL.