Click here to Skip to main content
15,895,656 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
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 :

// GET:       
        public ActionResult Edit(int id)
        {          
            var returnedMovie = _movieProxy.GetMovieById(id);

            // if not found
            if (returnedMovie == null)
                return HttpNotFound();
            
            RedirectToAction("EditConfirmed", "MovieMvc", id);
        }



        // POST:     
        [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 :

// GET:       
        public ActionResult Edit(int id)
        {          
            var returnedMovie = _movieProxy.GetMovieById(id);

            // if not found
            if (returnedMovie == null)
                return HttpNotFound();
            
            return View(returnedMovie);
        }



        // POST:     
        [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?
Posted
Updated 27-Nov-20 0:47am

1 solution

In general, don't try to use the same action for adding and editing records. Instead, have four actions:
  • C#
    [HttpGet] 
    public ActionResult Add()
    Display the form to add a record.
  • C#
    [HttpPost] 
    public ActionResult Add(MovieViewModel model)
    Validate the submitted data. Either re-display the add form, or add the record and redirect back to the index.
  • C#
    [HttpGet] 
    public ActionResult Edit(int movieId)
    Load the record and display the form to edit it.
  • C#
    [HttpPost] 
    public ActionResult Edit(MovieViewModel model)
    Validate the submitted data. Either re-display the edit form, or update the record and redirect back to the index.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900