【问题标题】:referencing namespace for JSON in ASP.NET MVC在 ASP.NET MVC 中引用 JSON 的命名空间
【发布时间】:2017-02-10 00:56:42
【问题描述】:

我正在尝试为我的 repository.cs 使用 json,但它一直给我错误“在当前上下文中不存在”。我想我缺少一个名称空间,但我不确定要使用哪个名称空间。我曾尝试使用命名空间“using system.web.mvc”,但它只纠正了“JsonRequestBehavior”,并在“Json”上显示“当前上下文中不存在”的错误消息

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

【问题讨论】:

    标签: c# asp.net json asp.net-mvc


    【解决方案1】:

    System.Web.Helpers 是您要查找的命名空间。

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

    【讨论】:

      【解决方案2】:

      您是否尝试过使用 Json.net?请参阅上一个问题的答案:

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

      编辑:跟进 DmiHawk 的回答:

         public class User: Controller {
      
      }
      

      【讨论】:

      • 我尝试使用他们建议的命名空间,但错误仍然显示
      猜你喜欢
      • 2017-08-15
      • 2011-07-17
      • 2010-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-13
      • 2015-11-10
      • 2010-11-13
      相关资源
      最近更新 更多