There are a few ways to pass parameters correctly from @Url.Action
to a method:
@Url.Action
. The method can then retrieve the values from the query string using the Request.QueryString
property.Example:
@Url.Action("MyAction", "MyController", new { id = 123, name = "John" })
public ActionResult MyAction(int id, string name)
{
// use id and name parameters
...
}
RouteConfig.cs
. The method can then retrieve the values from the route using the RouteData.Values
property.Example:
// RouteConfig.cs
routes.MapRoute(
name: "MyRoute",
url: "MyController/MyAction/{id}/{name}",
defaults: new { controller = "MyController", action = "MyAction", id = UrlParameter.Optional, name = UrlParameter.Optional }
);
// View
@Url.Action("MyAction", "MyController", new { id = 123, name = "John" })
// Controller
public ActionResult MyAction(int id, string name)
{
// use id and name parameters
...
}
Request.Form
property.Example:
// View
<form action="@Url.Action("MyAction", "MyController")" method="post">
<input type="text" name="id" value="123" />
<input type="text" name="name" value="John" />
<button type="submit">Submit</button>
</form>
// Controller
[HttpPost]
public ActionResult MyAction()
{
int id = int.Parse(Request.Form["id"]);
string name = Request.Form["name"];
// use id and name parameters
...
}
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2021-08-10 11:00:00 +0000
Seen: 15 times
Last updated: Mar 17 '22
What is the method for altering the color of the input text in a TextFormField in Flutter?
What is the procedure for utilizing the node-rdpjs library?
What is the method to change a PDF file to an Excel file using C#?
What is the method to make a TextButton inactive when the text field has no text?
What is the method to alter the background image in HTML?
What is the method for saving an entity with @EmbeddedId as its primary key in Hibernate?
What is the method to superimpose two seaborn density plots?