如何从MVC控制器返回JSON对象查看 [英] How to return Json object from MVC controller to view

查看:132
本文介绍了如何从MVC控制器返回JSON对象查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做的一个MVC应用程序,我需要从控制器传递JSON对象查看。

I am doing an MVC application where i need to pass json object from controller to view.

var dictionary = listLocation.ToDictionary(x => x.label, x => x.value);
return Json(new { values = listLocation}, JsonRequestBehavior.AllowGet);

以上code我用在我的控制器,现在当我部署视图页面其在浏览器中,打开一个下载对话框打开该文件时,它给了我,我需要的格式JSON对象。

The above code i am using in my controller , now when i deploy the view page its opening a download dialog in my browser , when open the file it gives me json object as i needed format.

现在我想回到我的看法页面还希望访问该视图页面JSON对象。我怎么能做到这一点。

Now i want to return my view page also want to access the json object in the view page. how can i do that.

推荐答案

当你做返回JSON(...)你特别告诉MVC的不使用视图的,并满足序列化JSON数据。您的浏览器打开一个下载对话框,因为它不知道如何处理这些数据做的。

When you do return Json(...) you are specifically telling MVC not to use a view, and to serve serialized JSON data. Your browser opens a download dialog because it doesn't know what to do with this data.

如果你不是想返回一个视图,只是做返回查看(...)像通常那样:

If you instead want to return a view, just do return View(...) like you normally would:

var dictionary = listLocation.ToDictionary(x => x.label, x => x.value);
return View(new { Values = listLocation });

然后在您的视图,只需连接code数据为JSON并将其分配给一个JavaScript变量:

Then in your view, simply encode your data as JSON and assign it to a JavaScript variable:

<script>
    var values = @Html.Raw(Json.Encode(Model.Values));
</script>


修改

下面是一个比较完整的示例。因为我没有从你有足够的背景下,这一样本将承担控制器,一个动作酒吧,和视图模型 FooBarModel 。另外,位置列表中是很难codeD:

Here is a bit more complete sample. Since I don't have enough context from you, this sample will assume a controller Foo, an action Bar, and a view model FooBarModel. Additionally, the list of locations is hardcoded:

控制器/ FooController.cs

public class FooController : Controller
{
    public ActionResult Bar()
    {
        var locations = new[]
        {
            new SelectListItem { Value = "US", Text = "United States" },
            new SelectListItem { Value = "CA", Text = "Canada" },
            new SelectListItem { Value = "MX", Text = "Mexico" },
        };

        var model = new FooBarModel
        {
            Locations = locations,
        };

        return View(model);
    }
}

型号/ FooBarModel.cs

public class FooBarModel
{
    public IEnumerable<SelectListItem> Locations { get; set; }
}

查看/美孚/ Bar.cshtml

@model MyApp.Models.FooBarModel

<script>
    var locations = @Html.Raw(Json.Encode(Model.Locations));
</script>

您的错误消息的外观,好像你是混合不兼容的类型(即 Ported_LI.Models.Locatioñ MyApp的。 Models.Location ),因此,要回顾一下,确保从什么从视图收到控制器动作侧发送匹配的类型。对于这个样本,特别是新FooBarModel 在视图控制器匹配 @model MyApp.Models.FooBarModel

By the looks of your error message, it seems like you are mixing incompatible types (i.e. Ported_LI.Models.Locatio‌​n and MyApp.Models.Location) so, to recap, make sure the type sent from the controller action side match what is received from the view. For this sample in particular, new FooBarModel in the controller matches @model MyApp.Models.FooBarModel in the view.

这篇关于如何从MVC控制器返回JSON对象查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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