为什么Json()函数未知 [英] Why Json() Function is unknown

查看:72
本文介绍了为什么Json()函数未知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码(在MVC3中):

I have the following code (in MVC3):

    public JsonResult GetTown(string term)
    {
        db = new SHAMUTEntities1();

            var data = db.towns.Where(t => t.name.Contains(term))
                .Take(10)
                .Select(t => new {label=t.name }).ToArray();

            return Json(data, JsonRequestBehavior.AllowGet);
        }            
    }

我收到以下错误:

System.Web.Helper.Json是一种类型,但是像变量一样使用

System.Web.Helper.Json is a type but is used like a variable

任何人都可以帮忙. 谢谢

Can anybody help with this. thanks

推荐答案

Json是ASP.NET MVC 3的System.Web.Mvc.Controller类的方法.由于它没有编译,因此您可能在不使用它的类中使用它.源自Controller.

Json is a method of the System.Web.Mvc.Controller class of ASP.NET MVC 3. Since it's not compiling, you are probably using it in a class that does not derived from Controller.

要修复此问题,只需从您的方法中返回data实例,然后在控制器方法中将其转换为JSON.

To fix it, just return the data instance from your method and convert it to JSON in a controller metod.

这篇关于为什么Json()函数未知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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