CS0103 C# The name 'Json' does not exist in the current context

前端 未结 3 1751

Well,

I have written JsonResult funtion. But i get the following error:

CS0103 C# The name \'Json\' does not exist in the current context

相关标签:
3条回答
  • 2021-01-11 13:43

    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

    0 讨论(0)
  • 2021-01-11 13:57

    I got it !

    I have forgotten to derive my class from Controller class

    public class User: Controller
    {
    
    }
    
    0 讨论(0)
  • 2021-01-11 13:59

    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;  
    }
    
    0 讨论(0)
提交回复
热议问题