Reputation: 95
I am trying to using json for my repository.cs but it keeps giving me the error "does not exist in current context". I think I am missing a namespace, but I am not sure which one to use. I have tried using the namespace "using system.web.mvc" but it only corrects the "JsonRequestBehavior" and gives an error message on "Json" that says "does not exist in current context"
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);
}
}
}
}
Upvotes: 1
Views: 1226
Reputation: 46
Have you tried using Json.net? Please see this answer to a previous question:
https://stackoverflow.com/a/31535517/6262021
Edit: following up on DmiHawk's answer:
public class User: Controller {
}
Upvotes: 0
Reputation: 599
System.Web.Helpers is the namespace you're looking for.
EDIT: You need to call that method from inside a class which inherits from System.Web.Mvc.Controller
Upvotes: 1