Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Json()返回null_C#_Json_Asp.net Mvc_Model View Controller - Fatal编程技术网

C# Json()返回null

C# Json()返回null,c#,json,asp.net-mvc,model-view-controller,C#,Json,Asp.net Mvc,Model View Controller,我只使用ASP.Net和MVC,没有其他库。 代码如下: //ExpensesController.cs - the controller public IActionResult getExpenses() { List<ExpensesViewModel> list = new List<ExpensesViewModel>(); string connectionString = "Data Source=DESKTOP-72RT825;I

我只使用ASP.Net和MVC,没有其他库。 代码如下:

//ExpensesController.cs - the controller
public IActionResult getExpenses()
{
    List<ExpensesViewModel> list = new List<ExpensesViewModel>();
    string connectionString = "Data Source=DESKTOP-72RT825;Initial Catalog=AccountingDB;Integrated Security=True;Pooling=False";
    SqlConnection sqlConnection = new SqlConnection(connectionString);
    sqlConnection.Open();
    SqlCommand query = new SqlCommand("Select * from Expenses", sqlConnection);
    try
    {
        SqlDataReader reader;
        reader = query.ExecuteReader();
        while (reader.Read())
        {
            String name = reader.GetValue(0).ToString();
            String value = reader.GetValue(1).ToString();
            String date = reader.GetValue(2).ToString();
            list.Add(new ExpensesViewModel() { Name = name, Date=date, Value = value });
            Debug.Print(name + " " + " " + value);
        }
    }
    catch (SqlException ex)
    {
        Debug.Print(ex.Message);
        return Json(ex.Message);
    }
    JsonResult jsonResult = null;
    try
    {
        jsonResult = Json(list);
    }
    catch(Exception ex)
    {
        Debug.Write(ex.Message);
    }
    return jsonResult;
}


//The View Model
public class ExpensesViewModel
{
    public string Name;
    public string Value;
    public string Date;
}
//ExpensesController.cs-控制器
公共IActionResult getExpenses()
{
列表=新列表();
string connectionString=“数据源=DESKTOP-72RT825;初始目录=AccountingDB;集成安全性=True;池=False”;
SqlConnection SqlConnection=新的SqlConnection(connectionString);
sqlConnection.Open();
SqlCommand query=newsqlcommand(“从费用中选择*”,sqlConnection);
尝试
{
SqlDataReader;
reader=query.ExecuteReader();
while(reader.Read())
{
字符串名称=reader.GetValue(0.ToString();
字符串值=reader.GetValue(1.ToString();
字符串日期=reader.GetValue(2.ToString();
添加(新的ExpensesViewModel(){Name=Name,Date=Date,Value=Value});
Debug.Print(名称++++值);
}
}
catch(SqlException-ex)
{
调试。打印(例如消息);
返回Json(例如消息);
}
JsonResult JsonResult=null;
尝试
{
jsonResult=Json(列表);
}
捕获(例外情况除外)
{
Debug.Write(例如消息);
}
返回jsonResult;
}
//视图模型
公共类费用VIEWMODEL
{
公共字符串名称;
公共字符串值;
公共字符串日期;
}

Json(list)
返回的数据是空的,即使列表不是空的,我在调试器中查看过,到数据库的连接良好,数据到达,它被放入列表中,但当我尝试将其转换为Json时失败。我已经尝试手动将元素添加到列表中,Json函数仍然返回null。

将视图模型更改为使用属性,而不是字段:

public class ExpensesViewModel
{
    public string Name { get; set; }
    public string Value { get; set; }
    public string Date { get; set; }
}

原因是默认模型绑定器绑定到具有公共getter/setter的属性。

将视图模型更改为使用属性,而不是字段:

public class ExpensesViewModel
{
    public string Name { get; set; }
    public string Value { get; set; }
    public string Date { get; set; }
}

原因是默认模型绑定器绑定到具有公共getter/setter的属性。

您使用的是哪个版本的mvc?您使用的是哪个版本的mvc?