2

I have two submit button in a form , one to search records corresponding a name , second one is to update that record in DB after doing correction if required.

Three action in controller

  1. Edit with HttpGet-- when first time view display it load all the name from DB to a dropdownlist
  2. Edit with HttpPost-- When we click search it fetch every detail of that name
  3. EditPerson HttpPost-- When we click Edit record It save All the Data Back to DB

In code I wanna do this

public ActionResult EditOperation(string Command, DataLayer objmodel)
{
    if (Command == "Search")
    {
        return RedirectToAction(Edit with Post method) // How to ? always going to Edit With get method
    }
    else if (Command == "Edit Record")
    {
        return RedirectToAction(EditPerson) //Easy to redirect but How to send Model object also ?
    }
    return View("Edit",objmodel);
}
RedirectToAction (String ActionName, String ControllerName , Object routeValues)

Is There any way to tell the complier that redirect to Edit Action but the one who have Post method not the Get ?
Note : Dont wanna use Javascript here , only pure c# code

Meer
  • 2,765
  • 2
  • 19
  • 28
Saurabh
  • 1,505
  • 6
  • 20
  • 37
  • you can annotate action to accept post verbs only i.e. [HttpPost], there would be only one submit in one form, I am wondering how you use two submits? – A.T. Feb 28 '17 at 06:01
  • Are you want to have 2 submit buttons in a form? If it's true that thing doesn't makes sense, a form should only POST to an action method. But you can modify `Edit` method to include saving record routine(s) depending on hidden field or other field values provided when user sends POST request. – Tetsuya Yamamoto Feb 28 '17 at 06:11
  • Multiple submit buttons can be used in one form , But thats not my concern , I want to redirect to a Action with Specific Method – Saurabh Feb 28 '17 at 06:11
  • I also tried to use separate forms for every submit button but in that case Model object which i am accessing in controller is showing null in second form. – Saurabh Feb 28 '17 at 06:13
  • Note that HTTP protocol uses PRG pattern (POST-redirect-GET) during submitting forms, hence redirecting POST method to another POST method doesn't makes sense. See this similar issue for further details: http://stackoverflow.com/questions/129335/how-do-you-redirect-to-a-page-using-the-post-verb. – Tetsuya Yamamoto Feb 28 '17 at 06:15
  • I tried to solver it by using action name [Actionname="EditPostMethod"] , But redirecting to EditPostMethod is showing error that cant find EditPostMethod – Saurabh Feb 28 '17 at 06:25

1 Answers1

2

Redirect always uses get, try to return the "Edit" view, passing in the Model you want to use:

public ActionResult Edit(){

....retrieve Model

return View(MyModel); // default view : Edit

}

[HttpPost]

public ActionResult Edit(MyModel){

....do things with MyModel

return View("OtherView", MyModel);

}
Samy
  • 125
  • 3
  • 6