2017-02-10 63 views
1

我想爲我的repository.cs使用json,但它一直給我錯誤「在當前上下文中不存在」。我想我缺少一個命名空間,但我不確定要使用哪一個。我一直在使用「使用system.web.mvc」的命名空間嘗試,但它只是糾正了「JsonRequestBehavior」,並在上面寫着「不在當前上下文中存在」在ASP.NET MVC中引用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); 
      } 
     } 
    } 
} 

回答

0

系統「的Json」給出了一個錯誤信息。 Web.Helpers是你正在尋找的命名空間。

編輯:您需要調用該方法從從System.Web.Mvc.Controller

+0

我用它嘗試過,但錯誤依然呈現 –

+0

見我的編輯響應 – Dmihawk

0

繼承您是否嘗試過使用Json.net一個類裏面呢?請參閱此回答上一個問題:

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

編輯:上DmiHawk的回答跟進:

public class User: Controller { 

} 
+0

我試着使用他們建議的命名空間,但錯誤仍然顯示 –

相關問題