I'm learning ASP.NET MVC 5 framework and web development.
t’s not clear to me how can I use the same action of a controller (e.g. Edit) to accomplish two different tasks by keeping track of the input parameter (let's say the parameter is the ID of an item) :
action1: insert info about the item (GET)
action 2: edit info about that item (POST).
Can I use a RedirectToAction and pass the second “edit action” (EditConfirmed) together with the parameter which I passed in the first one?
Or there is no need to do that and I just implement two "Edit" actions, the first one with GET and the second one with POST, by passing a different num of parameters?
In that case, can you explain to me how the framework works, i.e.: I call the View(itemModel) in the first Edit action Edit(itemId) and I'm redirected to the "Edit" View.
Now I have to insert the info about the item With Newvalues and I use the second Edit action with the Post method , Edit(id, itemWithNewvalues).
How does the framework know that the id to pass to this second action is the same I passed to the first one, without using a RedirectToAction?
What I have tried:
1) I tried it by using RedirectToAction :
public ActionResult Edit(int id)
{
var returnedMovie = _movieProxy.GetMovieById(id);
if (returnedMovie == null)
return HttpNotFound();
RedirectToAction("EditConfirmed", "MovieMvc", id);
}
[HttpPost]
public ActionResult EditConfirmed(int movieId, Movie movieWithNewValues)
{
try
{
_bookProxy.UpdateMovie(movieId, movieWithNewValues);
return RedirectToAction("Index");
}
catch
{
return RedirectToAction("Index");
}
}
2) I tried it without using RedirectToAction :
public ActionResult Edit(int id)
{
var returnedMovie = _movieProxy.GetMovieById(id);
if (returnedMovie == null)
return HttpNotFound();
return View(returnedMovie);
}
[HttpPost]
public ActionResult EditConfirmed(int movieId, Movie movieWithNewValues)
{
try
{
_movieProxy.UpdateMovie(movieId, movieWithNewValues);
return RedirectToAction("Index");
}
catch
{
return RedirectToAction("Index");
}
}
Which of the previous approaches is correct?