RedirectToAction from the inside [HttpPost] to [HttpGet] - parameters

Code below:

[HttpGet]
public ActionResult Edit(string id="")
{
    // ...
}

[HttpPost]
public ActionResult Edit(string itemId="", EditViewModel viewModel)
{
    // ...

    RedirectToAction("Edit", new { id = itemId });
}

returns an error: "Optional parameters must appear after all required parameters".

I assume it is trying to redirect the [HttpPost] action.

How to redirect to [HttpGet] action?

I am trying to implement a save function when it saves the edit and reloads the form with the new values.

+3
source share
1 answer

The error message is clear ...

... if you know that an optional parameter is a parameter with a default value (an empty string in your case)

[HttpPost]
public ActionResult Edit(EditViewModel viewModel, string itemId="")
{
    // ...

    RedirectToAction("Edit", new { id = itemId });
}

and you are done

+4
source