Monday, May 2, 2016

MVC Render Action on Post

Leave a Comment

I have a page that calls another partial view. The page loads fine, but when there is a validation error, it appears to call the post method multiple times.

The code that is causing the issue is here:

<div>     @{Html.RenderAction("ViewUploadedDocs", "TrackingHome", new { number = @Model.Id.ToString() });} </div> 

This should call the following method in the controller.

    public ActionResult ViewUploadedDocs(string number)     {         return PartialView();     } 

It is not decorated with [HttpGet] or [HttpPost]. The method that keeps getting called is below which is the post method of the page.

    [HttpPost]     [MultipleButton(Name = "action", Argument = "Save")]     public ActionResult Edit(EditScreenModelValidation model)     {         if (ModelState.IsValid)         {             return RedirectToAction("UserWorkflows", "Home", new { Area = "Workflow" });         }         return View("Edit", model);     } 

I have read on stackoverflow where people have the page calling the post method that they are trying to get, but mine is calling the post method of my main page and not the page that I am trying to get. If I remove the renderAction line in my main page, then the page works correctly, and the action does not call the Edit page in it.

1 Answers

Answers 1

RenderAction invokes the specified child action method and renders the result inline in the parent view (it calls the action). You should use RenderPartial if you need to pass the current ViewDataDictionary object or Partial if you need the specified view to be rendered as an HTML-encoded string, depending on what you're trying to accomplish.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment