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

2020-02-02 05:50发布

I can't see 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!

Thanks in advance!

edit: 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
        );

it appears someone has downvoted the two suggestions below but not posted their solution!

5条回答
beautiful°
2楼-- · 2020-02-02 06:36

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

查看更多
迷人小祖宗
3楼-- · 2020-02-02 06:44

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)%>
查看更多
家丑人穷心不美
4楼-- · 2020-02-02 06:48

In MVC 4 you can link from one view to another controller passing the Id or Primary Key via

@Html.ActionLink("Select", "Create", "StudentApplication", new { id=item.PersonId }, null) 
查看更多
Juvenile、少年°
5楼-- · 2020-02-02 06:48

On MVC 5 is quite similar

@Html.ActionLink("LinkText", "ActionName", new { id = "id" })
查看更多
爱情/是我丢掉的垃圾
6楼-- · 2020-02-02 06:53

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)
查看更多
登录 后发表回答