1

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 });
haldo
  • 14,512
  • 5
  • 46
  • 52
Shumba Soft
  • 97
  • 3
  • 7

3 Answers3

1

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

haldo
  • 14,512
  • 5
  • 46
  • 52
0
url: "{controller}/{action}/{id}

You didn't respect the routing configuration. Try:

@Html.ActionLink("DisplayUploadedFile", "DisplayUploadedFileContents", new { UploadId = item.UploadId })
sander
  • 719
  • 2
  • 9
  • 21
  • Thank you @sander you are right the conventions were not followed so I have sorted them out but I still get the same error. I think something else is missing – Shumba Soft Sep 26 '19 at 13:13
  • I edited the parameter name Also, you don't need to map the route. Just follow the default routing conventions. I think you mapping this route in the routeconfig is also interfering. – sander Sep 26 '19 at 13:43
  • `@Html.ActionLink("DisplayUploadedFile", "DisplayUploadedFileContents", new { UploadId = item.UploadId })` That is what I have now so I am not sure what I missing here – Shumba Soft Sep 26 '19 at 13:49
  • remove the mapRoute that you write for DisplayUploadedFileContents and keep (or restore) the original routing configuration in routeconfig.cs – sander Sep 26 '19 at 14:33
0
@Html.ActionLink("DisplayUploadedFile", "DisplayUploadedFileContents", new { UploadId = item.UploadId}, null )

You need the Argument for "htmlArgument"

Please Refer to: HTML.ActionLink method

Marsman
  • 16
  • 4