diegohb
diegohb

Reputation: 1887

MVC3 Multiple PartialViews in one View

The answer supplied here is exactly what i've got going on. But when I hit submit in either partialview, the output is just the html for the PartialView! I'm new to MVC3 so I'm sure I'm just missing something that I should have already learned.

 public ActionResult CreateV2Worksheet()
    {
        return PartialView("_NewV2WorksheetInput", new NewV2WorksheetInputModel());
    }

    [HttpPost]
    public ActionResult CreateV2Worksheet(NewV2WorksheetInputModel pNewV2Input)
    {
        if (ModelState.IsValid)
        {
            ModelState.AddModelError("ScreeningNumber", "random server err");
            return PartialView("_NewV2WorksheetInput", pNewV2Input);    
        }

        return PartialView("_NewV2WorksheetInput", pNewV2Input);
    }

Upvotes: 0

Views: 2239

Answers (2)

TheRightChoyce
TheRightChoyce

Reputation: 3084

I'm going to assume you're not submitting via AJAX, so thus you're doing a full postback. In that case your output makes sense: you're posting to the partial view, and then all you get back is the html for the partial view since, once the post is initiated, your html state is gone. Partials are just partials.. you can't post to a partial and expect to get the full output returned.

I'd recommend either posting to an Action that renders the whole page, or have the partial render either the full page view ( return FullPageView( someData ); ), or do a redirect to a full page view ( return Redirect( "FullPageview" ); ).

Alternatively, you could post via ajax and return a JsonResult then handle any UI changes on the client (jquery, etc). You should be able to do this using Ajax.BeginForm().. but that's not something I personally use so can't help there. There's a post here that shows how to take the raw partial output and update the UI. The other method is to return an object with the JsonResult with error handling and such, and parse that. I.E.

return Json( someReturnObjectThatYouDefinedThatMayAlsoHaveAnErrorState );

Upvotes: 1

Lasantha Bandara
Lasantha Bandara

Reputation: 809

Use return View("_NewV2WorksheetInput", pNewV2Input);

instead of return PartialView("_NewV2WorksheetInput", pNewV2Input);.

Then the partial view will be rendered into the Layout you have used for your current page. If you want to render the partial view into another Layout, put this on top of your PartialView.

@{
      Layout="~/Views/Shared/<Layout Name>.cshtml"
}

Upvotes: 0

Related Questions