0

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"; }

1 Answer 1

2

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)
    {
        <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.

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

2 Comments

Yet, there is a new problem, I've been trying to solve this some time and studied some literature about that, but nohing explains my problem . Everytime I click click on the ActionLink, 404 exception is thrown. No matter what I change in the controller. Any ideas what could be wrong?
There are so many things that could be wrong. Ask a new question and post your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.