Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to differentiate between actionresults

Tags:

c#

asp.net-mvc

My page load action result and http post action result both pass in the model.

    [Authorize]
    public ActionResult StepTwo(PostcodesModel model)
    {
        return View();
    }

    [Authorize]
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult StepTwo(PostcodesModel model)
    {
        return View();
    }

Since they both take in the model, what can I add to make them unique ?

like image 721
StevieB Avatar asked Nov 23 '25 19:11

StevieB


2 Answers

You should use ActionName attribute, it represents an attribute that is used for the name of an action. If it is not present the name of the method is used.

    [Authorize]
    public ActionResult StepTwo(PostcodesModel model)
    {
        return View();
    }

    [ActionName("StepTwo")]
    [Authorize]
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult StepTwoPost(PostcodesModel model)
    {
        return View();
    }
like image 96
Satpal Avatar answered Nov 26 '25 07:11

Satpal


I usually use FormCollection:

[Authorize]
public ActionResult StepTwo(PostcodesModel model)
{
    return View();
}

[Authorize]
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult StepTwo(PostcodesModel model, FormCollection additionalData)
{
    return View();
}
like image 44
CodingIntrigue Avatar answered Nov 26 '25 09:11

CodingIntrigue



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!