开发者

Why Json() Function is unknown

开发者 https://www.devze.com 2023-03-29 03:07 出处:网络
I have the following code (in MVC3): public JsonResult GetTown(string term) { db = new SHAMUTEntities1();

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

I get the following error:

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

Can anybody help wit开发者_JS百科h this. thanks


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.

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

0

精彩评论

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