开发者

.NET / jquery - previewing a form result/rendering a view to a string

Here's the situation (using MVC 2.0):

I'm trying to build a feature to allow a user to preview changes they make to their bio/profile without actually committing their changes first. User fills out a form, clicks a "Preview" button and see what their changes look like. One difficulty is the front-end has a different master-page, so we need to render the whole view, not just a control.

Here's the approach I took:

  1. Asynch post the serialized form to a controller action
  2. Manipulate the model to flesh out the collections, etc. that don't get posted
  3. Return the front-end view, passing it this modified model
  4. Catch the response to the asynch method, wrap it in an iframe and write that to a lightboxed div on the page

Code I'm using... Controller action (the BuildPreview method just alters the model slightly)

    [HttpPost]
    [Authorize]
    public ActionResult PreviewProfile(PersonModel model)
    {
        return View("Person", PeopleService.BuildPreview(model));;
    }

HTML/Jquery stuff:

<script type="text/javascript">
$(document).ready(function () {

    $("#previewButton").click(function (e) {
        $.post("/PreviewProfile", $("#bioForm").serialize(), function (response) {
            $("#previewFrame").html(response);
            $("#holdMyPreview").modal({
                                overlayClose: true,
                                escClose: true,
                      开发者_开发问答          autoResize: true,
                            }, "html");
        });
    });
});

The modal method is just a basic lightbox-esque thing.

Running into two problems:

  1. EDIT - removed this, I was accidentally pulling a child control
  2. The iframe isn't rendering the html (perhaps because it's not valid b/c it's missing html/body/head tags?). If I just drop the response direcltly into the div, without the iframe, it works... albiet with the wrong stylesheet. If I try to insert it into iframe it just treats it as an empty page, just the html, head and body tags show up.

Any thoughts?

Sam

PS: Tried this over at MSDN forums (http://forums.asp.net/t/1675995.aspx/1?Rendering+a+view+into+a+string+) and it didn't get anywhere, figured I'd see if SO has any brilliance.


so, just massage the response when you get it back, add the missing html/body/head

$.post("/PreviewProfile", $("#bioForm").serialize(), function (response) {
            response = "<html><body>"+response+"</body></html>";
            $("#previewFrame").html(response);
            $("#holdMyPreview").modal({
                                overlayClose: true,
                                escClose: true,
                                autoResize: true,
                            }, "html");
        });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜