我正在使用一个jQuery插件,需要一个具有以下结构的JSON对象(我将从数据库中检索值):
{ results: [
{ id: "1", value: "ABC", info: "ABC" },
{ id: "2", value: "JKL", info: "JKL" },
{ id: "3", value: "XYZ", info: "XYZ" }
] }
Run Code Online (Sandbox Code Playgroud)
这是我的班级:
public class results
{
int _id;
string _value;
string _info;
public int id
{
get
{
return _id;
}
set
{
_id = value;
}
}
public string value
{
get
{
return _value;
}
set
{
_value = value;
}
}
public string info
{
get
{
return …Run Code Online (Sandbox Code Playgroud) 如何返回一个json格式的类.
这个方法在控制器中运行很好但是当我想放入一个类时,Json对象似乎不存在.
public JsonResult Test()
{
//Error 1 The name 'Json' does not exist in the current context C:\inetpub\wwwroot\mvcinfosite\mvcinfosite\Validation\ValidationClass\BaseValidator.cs 66 20 mvcinfosite
return Json(new { errMsg = "test" });
}
Run Code Online (Sandbox Code Playgroud)
我想将该代码放在一个简单的类中.我希望能够在许多控制器中调用此方法.
谢谢.
编辑
这是我的班级(代码不起作用的地方)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using mvcinfosite.Models;
using mvcinfosite.Base;
using System.Web.Mvc;
public class BaseValidator
{
public JsonResult Test()
{
return Json(new { errMsg = "test" });
}
}
Run Code Online (Sandbox Code Playgroud)