Reputation: 975
Well,
I have written JsonResult funtion. But i get the following error:
CS0103 C# The name 'Json' does not exist in the current context
I cant find solution to it... plzz help?
public JsonResult DoUserExist(string Emailaddress)
{
bool ch = false;
string connectionString = ConfigurationManager.ConnectionStrings["FreelanceDBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand("GetCities", con);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
if (rdr != null)
{
ch = true;
}
}
return Json(ch,JsonRequestBehavior.AllowGet);
}
Upvotes: 21
Views: 51920
Reputation: 313
Page must be derived from Controller class to Json () and JsonResult() part by using Microsoft.AspNetCore.Mvc;
If you are using Razor page model then you can face this issue. I got it after too much time spending.
You can also change your page derived from Controller instead of pagemodel like this public class User: Controller
Upvotes: 9
Reputation: 975
I got it !
I have forgotten to derive my class from Controller class
public class User: Controller
{
}
Upvotes: 72
Reputation: 2000
first try this,
in your controller it should be ActionResult
not the JsonResult
and you should pass object as the data.
You can get string output as follow. In here getCities is the model similar to the table fields.
public string DoUserExist(string Emailaddress)
{
bool ch = false;
string connectionString = ConfigurationManager.ConnectionStrings["FreelanceDBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand("GetCities", con);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
con.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
List<getCities> _getCities= new List<getCities>();
while (reader.Read())
{
getCities cities= new getCities();
cities.Data.Add(int.Parse(reader["col2"].ToString()));
cities.Data.Add(int.Parse(reader["col3"].ToString()));
cities.Data.Add(int.Parse(reader["col4"].ToString()));
_getCities.Add(cities);
}
JavaScriptSerializer jss = new JavaScriptSerializer();
jsonString = jss.Serialize(_getCities);
}
}
return jsonString;
}
Upvotes: 0