views:

102

answers:

2

So I am following ScottGu's NerdDinner tutorials, and am having some trouble wrapping my head around this error. I am trying to implement CRUD, so at first, while editing data, I started with this code:

        public ActionResult Edit(int id)
    {
        Dinner dinner = dinnerRepository.GetDinner(id);
        return View(dinner);
    }

which displays the Edit View properly, and gives me fields to edit my data. Now, when I continue on and my Edit action turns into this:

        [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Edit(int id, FormCollection formValues) 
    {
    Dinner dinner = dinnerRepository.GetDinner(id);
    UpdateModel(dinner);
    dinnerRepository.Save();
    return RedirectToAction("Details", new { id = dinner.DinnerID });
    }

I return a 404 error. Apparently the Edit view cannot be found. I am having an extremely hard time understanding why this is happening. I even tried to copy the exact syntax (which I don't like to do.) but no luck. Am I missing something that obvious?

EDIT: I happens as soon as I add this line:

[AcceptVerbs(HttpVerbs.Post)]

maybe that will help?

+4  A: 

You need to have both methods in your controller.

The first method is for GET requests, requests which aren't the result of a form submission. These are the ones you see if you go /Edit/1 /Edit/2 etc.

The ActionResult Edit() method does not replace your first Edit() method but is an additional method which responses to POST requests which are the result of sending the form

blowdart
worked. Thanks a bunch. Can't believe I didn't catch that in the documentation.
BBetances
A: 

You actually have two Edit actions, do you? One should be w/o the [AcceptVerbs] attribute and is used to display the initial Edit view, the other action accepts only POST calls and is used to turn whatever values were posted from the form into a model and save that in the data store.

From your wording it appears like instead of adding a second Edit action (method) you edited the old one.

Pawel Krakowiak
that's exactly what I did. I should have read the tutorial a bit closer when he said to overload the action. Thanks everyone for the help.
BBetances