How to pass a parameter to a controller from a cshtml view in mvc

asp.net asp.net-mvc asp.net-mvc-routing c# entity-framework-6

Question

net MVC 4 I followed the microsoft tutorials on how to pass a parameter to a controller from a cshtml view in mvc and I keep getting an error that says the resource cannot be found.If I put a break point in the cshtml I can actually see the value of the Id but it is not hitting the controller at all seems like it cant find it

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested URL: /UploadLogs/DisplayUploadedFileContents/89

This is my controller method

public class DisplayUploadedFileController : Controller
{
    private MarketingDBEntitiesModel db = new MarketingDBEntitiesModel();
    // GET: DisplayUploadedFile
    public ActionResult DisplayUploadedFileContents(int UploadId)
    {
        return View(db.marketingdbclients_dataTable.OrderByDescending(r => r.ClientId).Where(r => r.ClientDataId < 1000).ToList());
       // return View();.
    }
}

My line in the cshtml

<td>    
    @*@Html.ActionLink("Details", "Details", new { id = item.UploadId })*@

    @Html.ActionLink("Details", "DisplayUploadedFileContents", new { id = item.UploadId })    
</td>

My route config

 routes.MapRoute(
      name: "DisplayUploadedFileContents",
      //url: "",
      url: "{controller}/{action}/{id}",
      defaults: new { controller = "DisplayUploadedFile", action = "DisplayUploadedFileContents", id = UrlParameter.Optional });
1
1
9/26/2019 2:52:00 PM

Accepted Answer

Making a couple of changes should get this working.

First, if you want to use the routing for the url like this {controller}/{action}/{id}, change the parameter name in the controller action from UploadId to id:

public ActionResult DisplayUploadedFileContents(int id)

Next, it looks like you're linking from a different controller since in the error the requested URL is /UploadLogs/DisplayUploadedFileContents/89 (note UploadLogs is not DisplayUploadedFile).

Linking to the DisplayUploadedFile controller from a view that belongs to a different controller, you will need to use this overload taking 5 parameters:

@Html.ActionLink("Display File Contents", "DisplayUploadedFileContents", "DisplayUploadedFile", 
                     null, new { id = item.UploadId })

However, if you're accessing the controller from a view within the same controller you should be able to use this overload for ActionLink taking 3 parameters:

@Html.ActionLink("Display File Contents", "DisplayUploadedFileContents", new { id = item.UploadId })

Please refer to the ActionLink documentation

0
9/26/2019 2:27:59 PM

Popular Answer

@Html.ActionLink("DisplayUploadedFile", "DisplayUploadedFileContents", new { UploadId = item.UploadId}, null )

You need the Argument for "htmlArgument"

Please Refer to: HTML.ActionLink method



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow