user1250264
user1250264

Reputation: 905

ASP.NET MVC HttpPost return View() cause error since it can't find cshtml

I am writing HttpGet and HttpPost methods in my Controller. I got the HttoPost methods working but I have some questions about the redirect from HttpPost to the HttpGet methods.

[HttpGet]
public async Task<ActionResult> GetData(MyViewModel model)
{
    // Get Data 

    return View(model); 
}



[HttpPost]
public async Task<ActionResult> UpdateData(MyViewModel model)
{
            try
            {
                if (ModelState.IsValid)
                {
                    // Update updatedModelObject

                    //return RedirectToAction("GetData", updatedModelObject);
                    return RedirectToAction("GetData");
                }
            }
            catch (Exception)
            {
            }

            //return RedirectToAction("GetData", "MyController");

            //return RedirectToAction("GetData");

            return View(model);
}

When I click the submit button on the page, it calls the HttpPost UpdateData method. If the ModelState.IsValid, it update the database and calls the RedirectToAction("GetData"). It does work and calls the HttpGet method to make another call from the database. Here is my problem, if the ModelState.IsValid is false, it will call View(model) which cause the error:

The view 'UpdateData' or its master was not found or no view
engine supports the searched locations. The following locations were
searched:
~/Views/MyController/UpdateData.cshtml
~/Views/MyController/UpdateData.vbhtml
~/Views/Shared/UpdateData.cshtml
~/Views/Shared/UpdateData.vbhtml
~/__MVCSITEMAPPROVIDER/UpdateData.ascx

I have been reading many posts where the example HttpGet and HttpPost method are both named Index() so it will not get the same error like I got. I used different HttpGet and HttpPost method names so it was looking for a UpdateData.cshtml. From the posts I read, they say if Model.IsValid is true call the HttpGet with the updated model to pass. If Model.IsValid is false, the posts are saying call View(model) to pass the same model data.

Here are my questions.

  1. What should I call instead of View(model) to pass the View with the same model to refresh the View the same model data and avoid the error? As the code as it is, its looking for UpdateData.cshtml

  2. If ModelState.IsValid is true, it will update database and call HttpGet to fetch the refreshed data from the database and pass the model to the View in GetData. But if ModelState.IsValid is false, it calls View(model). Does that call HttpGet or just supposed to refresh the View with the old model data?

Thanks.

Upvotes: 3

Views: 2448

Answers (1)

NikolaiDante
NikolaiDante

Reputation: 18649

What should I call instead of View(model) to pass the View with the same model to refresh the View the same model data and avoid the error? As the code as it is, its looking for UpdateData.cshtml

Instead of return View(model); pass the view name in to return to, with the model:

return View("GetData", model);

if ModelState.IsValid is false, it calls View(model). Does that call HttpGet or just supposed to refresh the View with the old model data?

The populated model gets rendered back to the user with any ModelState info so that they can fix validation.

The GET action isn't processed again.


This article has more information on how to implement POST - REDIRECT - GET.

Upvotes: 5

Related Questions