简体   繁体   中英

ASP.NET MVC passing an ID in an ActionLink to the controller

I can't seem to retrieve an ID I'm sending in a html.ActionLink in my controller, here is what I'm trying to do

<li>
    <%= Html.ActionLink("Modify Villa", "Modify", "Villa", new { @id = "1" })%></li>


    public ActionResult Modify(string ID)
    {

        ViewData["Title"] =ID;
        return View();
    }

That's what a tutorial I followed recommended, but it's not working, it's also putting ?Length=5 at the end of the URL!

Here is the route I'm using, it's default

        routes.MapRoute(
            "Default",                                              // Route name
            "{controller}/{action}/{id}",                           // URL with parameters
            new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
        );

Doesn't look like you are using the correct overload of ActionLink. Try this:-

<%=Html.ActionLink("Modify Villa", "Modify", new {id = "1"})%>

This assumes your view is under the /Views/Villa folder. If not then I suspect you need:-

<%=Html.ActionLink("Modify Villa", "Modify", "Villa", new {id = "1"}, null)%>

在 MVC 4 中,您可以从一个视图链接到另一个通过 Id 或主键传递的控制器

@Html.ActionLink("Select", "Create", "StudentApplication", new { id=item.PersonId }, null) 

Don't put the @ before the id

new { id = "1" }

The framework "translate" it in ?Lenght when there is a mismatch in the parameter/route

在 MVC 5 上非常相似

@Html.ActionLink("LinkText", "ActionName", new { id = "id" })

The ID will work with @ sign in front also, but we have to add one parameter after that. that is null

look like:

@Html.ActionLink("Label Name", "Name_Of_Page_To_Redirect", "Controller", new {@id="Id_Value"}, null)

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