MVC3 Multiple PartialViews in one View

孤者浪人 提交于 2019-12-14 03:09:42

问题


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);
    }

回答1:


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 );




回答2:


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"
}


来源:https://stackoverflow.com/questions/9677818/mvc3-multiple-partialviews-in-one-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!