...">

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

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

The line above creates a hyperlink to represent Razor. The html helper method MyActionHref () creates a link. When you click on the link, it calls the action method from the controller. Now suppose that the action controller method that calls this connection is parameterized, i.e. public ActionResult MyAction(string myParams){}
(The html helper method MyActionHref () is even overloaded to accept three parameters respectively.)

How can this additional parameter be passed to the controller action method from the model?
Let's say

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

Any suggestions?

+4
source share
2 answers

Why do you use such an assistant when you can simply:

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

which will generate the correct anchor tag:

  <a href="/MyController/MyAction?myParams=Hello">some text</a> 
+20
source

You can use the Url.Action method or copy its behavior.

 @Url.Action( controllerName: "MyController", actionName: "MyAction", routeValues: new { id = 1 } ); 

Look at the signature of this method to find what you want to do.

+7
source

Source: https://habr.com/ru/post/1345846/


All Articles