我不完全确定如何定义这个问题,但基本上我正在开发一个ASP.Net应用程序,我正在生成一个名为JsonResult
的{{1}}。
我的代码如下:
IndexJson
这适用于返回以下JSON:
public JsonResult IndexJson()
{
var contacts = (from x in db.ContactSet
select new
{
x.AccountId,
x.FirstName,
x.LastName,
x.FullName,
x.JobTitle,
x.ParentCustomerId,
x.EMailAddress1,
x.Telephone1,
x.MobilePhone,
x.Fax,
x.GenderCode,
x.BirthDate
}).ToList();
return Json(contacts, JsonRequestBehavior.AllowGet);
}
但是现在我想返回以下JSON(现在硬编码,我稍后会更改值):
[{/*contact info*/}, {/*contact info*/}, {/*contact info*/}, ...]
我如何调整代码来做到这一点?
答案 0 :(得分:4)
简单地将所有内容包装到新的匿名对象
中return Json(new { current = 1, rowCount = 10, rows = contacts, total = 1123 },
JsonRequestBehavior.AllowGet
);
答案 1 :(得分:1)
看起来您至少使用了jQuery Bootgrid之类的jQuery网格,至少是您需要的输出外观。
如果是这种情况,您可以执行以下操作。
来到控制器的输入
public class ResponseData<T> where T : class
{
public int current { get; set; } // current page
public int rowCount { get; set; } // rows per page
public T rows { get; set; } // items
public int total { get; set; } // total rows for whole query
}
您需要的输出
public JsonResult IndexJson(RequestData model)
{
var contacts = (from x in db.ContactSet
select new
{
x.AccountId,
x.FirstName,
x.LastName,
x.FullName,
x.JobTitle,
x.ParentCustomerId,
x.EMailAddress1,
x.Telephone1,
x.MobilePhone,
x.Fax,
x.GenderCode,
x.BirthDate
}).ToList();
var tResult =
ResponseData<YourObjectType>()
{
current = model.current,
rowCount = model.rowCount,
rows = contacts,
total = contacts.Count
};
return Json(tResult, JsonRequestBehavior.AllowGet);
}
group = 'se.thinkcode.gradle'
version = '1.0.0-SNAPSHOT'
apply plugin: 'groovy'
apply plugin: 'maven'
sourceCompatibility = 1.8
dependencies {
compile gradleApi()
compile 'com.google.code.gson:gson:2.8.0'
testCompile 'junit:junit:4.12'
}
repositories {
mavenCentral()
mavenLocal()
}
// try to add GSON to plugin dependencies
project.dependencies.ext.pluginDeps = {
project.fileTree("C:\\Users\\user\\Downloads")
}
dependencies {
compile pluginDeps()
}
// try to add GSON to plugin dependencies
project.configurations.compile.allDependencies;
// try to add GSON to plugin dependencies
project.buildscript {
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath "net.saliman:gradle-cobertura-plugin:1.1.2"
classpath 'com.google.code.gson:gson:2.8.0'
}
}
答案 2 :(得分:0)
您可能希望定义一个(通用)类型来存储您的额外字段和项目列表
public class ItemsWithTotal<T> // for pete sake find a better name!
{
public int Current {get; set; }
public int RowCount {get; set; }
public int Total {get; set; }
public List<T> Rows {get; set; }
}
用法是
var contacts = (from x in db.ContactSet
select new
{
x.AccountId,
x.FirstName,
x.LastName,
x.FullName,
x.JobTitle,
x.ParentCustomerId,
x.EMailAddress1,
x.Telephone1,
x.MobilePhone,
x.Fax,
x.GenderCode,
x.BirthDate
}).ToList();
var result = new ItemsWithTotal<ContactSet>(){
Current = 1,
RowCount = 10,
Total = 1123,
Rows = contacts
}
return Json(result);
答案 3 :(得分:0)
您可以创建看起来相同的视图模型。即
public MyReturnModel{
public int Current {get; set;}
public int rowCount {get; set;}
public List<contacts> rows {get; set;}
public int total {get; set;}
}
然后在你的方法中,只需适当地分配每个属性并返回JSONify新视图模型并返回它。