简体   繁体   中英

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

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 });
url: "{controller}/{action}/{id}

You didn't respect the routing configuration. Try:

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

You need the Argument for "htmlArgument"

Please Refer to: HTML.ActionLink method

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM