首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在ASP.NET MVC中引用JSON命名空间

在ASP.NET MVC中引用JSON命名空间
EN

Stack Overflow用户
提问于 2017-02-10 00:56:42
回答 2查看 1.1K关注 0票数 1

我试图在我的repository.cs中使用json,但是它一直给我错误“在当前上下文中不存在”。我想我缺少了一个名称空间,但我不确定要使用哪个名称空间。我尝试使用名称空间“使用system.web.mvc”,但它只更正了"JsonRequestBehavior“,并在"Json”上给出了一条错误消息,该消息表明“在当前上下文中不存在”。

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Dapper;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using Mis324Assignments.Models;

namespace Mis324Assignments.DataRepository
{
    public class BirdRepository
    {
        private SqlConnection connection;

        //Constructor to handle connection    
        public BirdRepository()
        {
            string connectString = ConfigurationManager.ConnectionStrings["Birds"].ToString();
            connection = new SqlConnection(connectString);
        }

        //To view all person details using generic list       
        public List<BirdModel> GetRandom()
        {
            using (IDbConnection db = connection)
            {
                string sql = "SELECT TOP 4 Id, Name, ImageName, Description FROM BirdDetails ORDER BY newid()";
                List<BirdModel> birds = db.Query<BirdModel>(sql).ToList();
                return birds;
            }
        }

        //Get one person by id (for update & details)
        public BirdModel GetOneBird(int id)
        {
            using (IDbConnection db = connection)
            {
                string sql = "select Id, Name, ImageName, Description FROM BirdDetails where id = @Id";
                //need to parameterize ID to avoid sql injection attacks.
                BirdModel bird = db.Query<BirdModel>(sql, new { id }).SingleOrDefault();
                return bird;
            }
        }

        public List<ColorModel> GetColors()
        {
            using (IDbConnection db = connection)
            {
                string sql = "SELECT ColorID, Name FROM Colors ORDER BY Name";
                List<ColorModel> colors = db.Query<ColorModel>(sql).ToList();
                return colors;
            }
        }

        public List<BirdModel> GetByColor(string colorID)
        {
            using (IDbConnection db = connection)
            {
                string sql = "select d.Id, d.Name, d.ImageName " 
                            +" from BirdDetails d, birdColors c " +" where d.Id = c.Id " 
                            +" and c.ColorID = @colorID " 
                            +" order by d.Name";
                List<BirdModel> birds = db.Query<BirdModel>(sql, new { colorID }).ToList();
                return birds;  

            }
        }

        public List<BirdModel> SearchName(string id)
        {
            using (IDbConnection db = connection)
            {
                string wildcard = '%' + id + '%';
                string sql = "select Id, Name, ImageName from BirdDetails where Name like @wildcard";
                List<BirdModel> birds = db.Query<BirdModel>(sql, new { wildcard }).ToList();
                return Json(birdRep.SearchName(id), JsonRequestBehavior.AllowGet);
            }
        }
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-02-10 01:04:05

System.Web.Helpers是您要寻找的名称空间。

编辑:您需要在继承自System.Web.Mvc.Controller的类中调用该方法

票数 1
EN

Stack Overflow用户

发布于 2017-02-10 01:05:03

你试过使用Json.net吗?请参阅对前一个问题的答复:

https://stackoverflow.com/a/31535517/6262021

编辑:跟进DmiHawk的回答:

代码语言:javascript
复制
   public class User: Controller {

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42149752

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档