model-view-controllerhtml.actionlink

How to create a string for @Html.ActionLink that concatenates an integer from the Model and a string


I'm using VS2015, MVC 5 design model. Creating a link that says "View" to open a PDF file in a new browser tab. It works fine, but the document sub-directory is hard-coded in the controller. I need to pass the document sub-directory + filename to the controller. The document sub-directory is the same as the Model.id . I'm having a difficult time converting the Model.id to a string and concatenating with the filename.
The following code in the view works fine with the hard-coded sub-directory in the controller

<td>@Html.ActionLink("View", "ViewAttachedDoc", "Documents", new { filename = item.filename}, new { target = "_blank" })</td>

But this code does not work

<td>@Html.ActionLink("View", "ViewAttachedDoc", "Documents", new { filename = Convert.ToString(Model.id) + "\" + item.filename }, new { target = "_blank" })</td>

The controller action is:

    public FileResult ViewAttachedDoc(string filename)
    {
        string DocPath = ConfigurationManager.AppSettings["DocPath"];
        string path = Path.Combine(DocPath, filename);
        return File(path, "application/pdf");
    }

TIA, Tracy


Solution

  • The issue is likely from trying to pass a backslash through the URL. This link of a similar question has that same problem and their solution was to use HttpUtility.UrlEncode(value); and HttpUtility.UrlDecode(value);. Otherwise if that still doesn't work, what error are you getting? P.S. C# automatically converts / -> \ for retrieving files.