ASP.NET MVC Passing models by ActionLink

后端 未结 4 759
广开言路
广开言路 2020-12-14 07:53

I would like to pass a model and an int to a controller upon clicking an ActionLink.

@Html.ActionLink(\"Next\", \"Lookup\", \"User\         


        
4条回答
  •  孤街浪徒
    2020-12-14 08:06

    You can't use an ActionLink to pass properties with a Link but you can do the following to get the same behavior.

    If you create a helper to generate these GET forms, you will be able to style them like they are regular link buttons. The only thing I caution against is that ALL forms on the page are susceptible to modification so I wouldn't trust the data. I'd rather just pull the data again when you get to where you are going.

    I use the technique above when creating search actions and want to retain a search history and keep the back button working.

    Hope this helps,

    Khalid :)


    P.S.

    The reason this works.

    @Html.ActionLink("Next", "Lookup", "User", Model.UserLookupViewModel, null)
    

    Is because the parameter list of the ActionLink method is generalized to take an object, so it will take anything. What it will do with that object is pass it to a RouteValueDictionary and then try to create a querystring based on the properties of that object.

    If you say that method is working above, you could also just try adding a new property to the viewmodel called Id and it will work like you wanted it to.

提交回复
热议问题