How to send data through actionlink in asp.net?

瘦欲@ 提交于 2021-02-07 19:41:34

问题


In the application that I am trying to make, I have a menu in my view as below:

<ul id="menu">
    <li>@Html.ActionLink("Create Project", "Display", "CreateDisplay")</li>
    <li>@Html.ActionLink("Open", "About", "Home")</li>
</ul>

where CreateDisplay is the name of a controller and Display is a method in that controller which will call another view.

But, I want the Display method to accept certain parameters like username of the person. In the current view, I have obtained the username as @ViewData["UserName"]

But I am not able to pass these values using ActionLink. I tried passing the parameters as

<li>@Html.ActionLink("Create Project", "Display?UserName="+@ViewData["UserName"], "CreateDisplay")</li>

but received an exception.

Please suggest some ways of doing this.

Thank you very much in advance.


回答1:


You need to use the overload that accepts parameters for route values. See the overloaded method ActionLink() here.

@Html.ActionLink(
    "Click me!",
    "Display",
    "CreateDisplay",
    new { UserName = "SetYourValHere" },
    null);


来源:https://stackoverflow.com/questions/8803928/how-to-send-data-through-actionlink-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!