Reputation: 25
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)
{
<tr>
<td class=" row col-md-12 "><a href="#" target="_blank"> <h5> @item</h5></a></td>
</tr>
}
</tbody>
</table>
And Controller code:
List<string> ftpfiles = new List<string>();
try
{
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)
{
ftpfiles.Add(simpleDirectoryListing[i]);
}
}
ViewBag.files = soubory;
}
catch { ViewBag.files = "nenacteno"; }
Upvotes: 0
Views: 1384
Reputation: 1092
It is not clear what the interaction should be when the user clicks on a row. Do you want:
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)
{
<tr>
<td>@Html.ActionLink(item, "Download", "YourController", new { id = item }, null)
</tr>
}
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.
Upvotes: 2