Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing value to a HREF in a Razor view in ASP.Net MVC3

   <a href="@(Html.MyActionHref(controllerName: "MyController", actionName: "MyAction"))">

The above line generates a hyperlink for Razor view. The html helper method MyActionHref() creates the link. On clicking the link it calls an action method from controller. Now suppose the action controller method which this link calls is parameterized i.e.
public ActionResult MyAction(string myParams){}
(The html helper method MyActionHref() is even overloaded to accept three parameters accordingly.)

How this additional parameter can be passed to the controller action method from the model?
Say,

<a href="@(Html.MyActionHref(controllerName: "MyController", actionName: "MyAction",params: new {....} }))">  

Any suggestions?

like image 419
SocialCircus Avatar asked Nov 28 '22 18:11

SocialCircus


1 Answers

Why are you using such helper when you can simply:

@Html.ActionLink(
    "some text", 
    "MyAction", 
    "MyController", 
    new { myParams = "Hello" }, 
    null
)

which will generate the proper anchor tag:

 <a href="/MyController/MyAction?myParams=Hello">some text</a>
like image 113
Darin Dimitrov Avatar answered Jan 02 '23 10:01

Darin Dimitrov