user3943563
user3943563

Reputation:

Can we change Action Method name in ASP.NET MVC Application?

This is two action method in controller with same name, i want to change this action name using attribute.

[HttpGet] 
public ActionResult Show()
{
    return View();
}

[HttpPost] 
public ActionResult Show(FormCollection frm)
{
    return View();
}

Upvotes: 5

Views: 12506

Answers (2)

Sleepy Panda
Sleepy Panda

Reputation: 458

You can use ActionName attribute.

[HttpPost, ActionName("Show")]
public ActionResult PostShow()
{
    // your code...
}

Upvotes: 21

Shyju
Shyju

Reputation: 218952

You can have the same name, but make sure that the method signature is different. To do that, you can simply add a parameter to your post method.

[HttpGet] 
public ActionResult Show()
{
    return View();
}

[HttpPost] 
public ActionResult Show(string name)
{
    return View();
}

Now when the Show form is submitted the input field with name value name will be submitted to the HttpPost action method.

Upvotes: 2

Related Questions