有一种观点认为MVC3多PartialViews [英] MVC3 Multiple PartialViews in one View

查看:161
本文介绍了有一种观点认为MVC3多PartialViews的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

答案提供这里正是我有事情。但是,当我打提交任何partialview,产量仅仅为PartialView的HTML!我是新来MVC3,所以我敢肯定,我只是失去了一些东西,我应该已经学会。

 公众的ActionResult CreateV2Worksheet()
    {
        返回PartialView(_ NewV2WorksheetInput,新NewV2WorksheetInputModel());
    }    [HttpPost]
    公众的ActionResult CreateV2Worksheet(NewV2WorksheetInputModel pNewV2Input)
    {
        如果(ModelState.IsValid)
        {
            ModelState.AddModelError(ScreeningNumber,随机服务器错误);
            返回PartialView(_ NewV2WorksheetInput,pNewV2Input);
        }        返回PartialView(_ NewV2WorksheetInput,pNewV2Input);
    }


解决方案

我要假设你没有通过AJAX提交,所以这样你做一个完整的回发。在这种情况下,你的输出是有道理的:你发布到局部视图,然后你得到的回复是对局部视图,因为一旦后期开始后,你的HTML状态消失的HTML。谐音只是谐音..你不能张贴到局部,并期望得到的全部输出返回。

我建议要么张贴到呈现整个页面,或有一个动作的部分渲染为完整页面视图(返回FullPageView(someData); ),或做重定向到一个全页视图(返回重定向(FullPageview); )。

另外,你可以通过AJAX发布,并返回一个 JsonResult 然后处理客户端(jQuery的,等等)上的任何用户界面的变化。你应该能够做到这一点使用Ajax.BeginForm()..但是这不是我个人使用,因此不能帮助那里。有一个帖子 ,说明如何采取原始部分输出并更新UI。另一种方法是用错误处理和返回等与 JsonResult 的对象,解析。即。

返回JSON(someReturnObjectThatYouDefinedThatMayAlsoHaveAnErrorState);

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

解决方案

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

这篇关于有一种观点认为MVC3多PartialViews的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆