返回从ASP.NET MVC操作部分景观和JSON [英] Return Partial View and JSON from ASP.NET MVC Action

查看:81
本文介绍了返回从ASP.NET MVC操作部分景观和JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我介绍KnockoutJS到现有的应用程序。我的计划是修改/使用,我们已经创建了现有的部分观点并将其绑定与淘汰赛的声明属性JS视图模型。当我做一个AJAX调用一个动作,最好我想的行动回报局部视图和JSON对象双方的HTML。然后,我可以填补与HTML的div的JSON转换为淘汰赛对象,并将其绑定到HTML。但我无法弄清楚如何既从操作返回。

I'm introducing KnockoutJS into an existing app. My plan is to modify/utilize the existing partial views that we've already created and bind them to the JS view models with Knockout's declarative attributes. When I make an AJAX call to an action, ideally I'd like the action to return both the HTML of the partial view and JSON object. Then I can fill a div with the HTML, convert the JSON to a Knockout object and bind it to the HTML. But I can't figure out how to return both from the action.

我需要完整的视图模型,因为我将更新它,并最终将其发送回服务器。

I need the full view model because I'll be updating it and eventually sending it back to the server.

我想过有动作返回局部视图(已绑定到模型)和局部视图中,包含JavaScript到.NET模型转换为淘汰赛对象。但我觉得周围散射JS喜欢就是混乱和难以维护。我宁愿一切都接近原来的Ajax调用。

I thought about having the action return the partial view (already bound to the model), and within the partial view, include javascript to convert the .Net model into a Knockout object. But I feel that scattering the JS around like that is messy and unmaintainable. I'd rather have everything close to the original ajax call.

我想另一种选择是使两项行动电话。一个用于JSON和另一个用于局部视图。但必须有一个滑头的方式。

I guess another alternative is to make two action calls. One for the JSON, and another for the partial view. But there has to be a slicker way.

对如何最好地做到这一点任何想法?

Any ideas on how best to do this?

推荐答案

我敢肯定有很多种方法可以做到这一点。我手动渲染从控制器的观点,再经过渲染视图早在我的JSON响应的一部分。

I'm sure there are a variety of ways to do this. I manually render the view from the controller, and then pass the rendered view back as part of my JSON response.

这preserves每个实体的责任。次使用的视图发动机仍然位于并且它们可以被重复使用。该控制器知道超出其名称及型号类型的观点很少,甚至没有。

This preserves the responsibilities of each entity. Views are still located using the view engine and they can be reused. The controller knows little or nothing about the view beyond its name and model type.

public static class RenderHelper
{
    public static string PartialView( Controller controller, string viewName, object model )
    {
        controller.ViewData.Model = model;

        using( var sw = new StringWriter() )
        {
            var viewResult = ViewEngines.Engines.FindPartialView( controller.ControllerContext, viewName );
            var viewContext = new ViewContext( controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw );

            viewResult.View.Render( viewContext, sw );
            viewResult.ViewEngine.ReleaseView( controller.ControllerContext, viewResult.View );

            return sw.ToString();
        }
    }
}

在你的操作方法:

object model = null; // whatever you want
var obj = new { 
    someOtherProperty = "hello", 
    view = RenderHelper.PartialView( this, "_PartialName", model ) 
};

return Json( obj );

请注意,我返回一个匿名类型。你可以返回任何(可序列化)类型你想要的,只要它有渲染视​​图的字符串属性。

Note that I'm returning an anonymous type. You can return any (serializable) type you want, as long as it has a string property for the rendered view.

测试使用人工绘制的操作需要稍加修改。这是由于渲染视图更早一点会比在MVC管道被渲染。

Testing an action that uses manual rendering requires a slight modification. This is due to rendering the view a bit earlier than it would be rendered in the MVC pipeline.

手册渲染


  1. 输入的操作方法

  2. 渲染视图明确< - 这将使它很难测试呼叫行动

  3. 退出操作方法

自动渲染


  1. 输入的操作方法

  2. 创建一个视图结果

  3. 退出操作方法

  4. 进程视图结果(从而渲染视图)

在换句话说,我们的手工绘制过程揭开序幕多种,使其难以测试(如与构建管理交互编译视图)等操作。

In other words, our manual rendering process kicks off a variety of other operations that make it difficult to test (such as interacting with the build manager to compile the view).

假设你要测试的操作方法,而不是观点的实际内容,您可以检查在托管环境中的code是否执行。

Assuming you wish to test the action method and not the actual contents of the view, you can check whether or not the code is executing in a hosted environment.

    public static string PartialView( Controller controller, string viewName, object model )
    {
        // returns false from a VS 2013 unit test, true from IIS
        if( !HostingEnvironment.IsHosted )
        {
            // return whatever you want here
            return string.Empty;
        }

        // continue as usual
     }

检查 HostingEnvironment.IsHosted 廉价(引擎盖下,它只是一个空检查)。

Checking HostingEnvironment.IsHosted is inexpensive (under the hood, it is simply a null check).

这篇关于返回从ASP.NET MVC操作部分景观和JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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