I am new to angularjs and ASP.NET webapi. I am working on some requirement which my base tables are as below
我对angularjs和ASP很陌生。净之前。我正在处理一些基本表如下所示的需求
CurriculumID SubjectArea CourseNumber
------------ ----------- ------------
303 GHIJ 101
304 ABCD 102
305 MNPQ 103
306 WXYZ 104
lookupId lookupValue
-------- -----------
1 Very Useful
2 Somewhat Useful
3 Not Useful
4 Not Applicable
5 Elsewhere
I have created two model classes (course and lookup) for these tables. How can i genarate JSon data as below in the backed using webapi Controller method public HttpResponseMessage getData(...)
我为这些表创建了两个模型类(course和lookup)。如何使用webapi Controller方法public HttpResponseMessage getData(…)实现如下所示的JSon数据
I need the resultant JSON data like as below
我需要得到如下所示的JSON数据
$scope.questions = [
{
"CurriculumID": "303", "SubjectArea": "GHIJ", "CourseNumber": "101", "answers": [
{ "lookupValue": "Very Useful","lookupId":"1" },
{ "lookupValue": "Somewhat Useful", "lookupId": "2" },
{ "lookupValue": "Not Useful", "lookupId": "3" },
{ "lookupValue": "Not Applicable", "lookupId": "4" },
{ "lookupValue": "Elsewhere", "lookupId": "5" }
]
},
{
"CurriculumID": "304", "SubjectArea": "ABCD", "CourseNumber": "102", "answers": [
{ "lookupValue": "Very Useful","lookupId":"1" },
{ "lookupValue": "Somewhat Useful", "lookupId": "2" },
{ "lookupValue": "Not Useful", "lookupId": "3" },
{ "lookupValue": "Not Applicable", "lookupId": "4" },
{ "lookupValue": "Elsewhere", "lookupId": "5" }
]
}
.
.
.
];
Please look into the this link for better understanding https://plnkr.co/edit/73oA3rsrre8gqYX9V25W?p=preview
请查看这个链接,以便更好地理解https://plnkr.co/edit/73oa3rsrre8gqyx9v25w? p预览。
So how can write the two methods getData(to generate JSon data). please can anyone help me to serialize this structure in ASP.NET
因此如何编写这两个方法getData(生成JSon数据)。请大家帮助我在ASP.NET中序列化这个结构。
https://plnkr.co/edit/73oA3rsrre8gqYX9V25W?p=preview
https://plnkr.co/edit/73oA3rsrre8gqYX9V25W?p=preview
1 个解决方案
#1
1
Let's suppose that you have modeled your SQL tables with the following entities:
假设您已经用以下实体为SQL表建模:
public class Question
{
[Key]
public int CurriculumID { get; set; }
public string SubjectArea { get; set; }
public int CourseNumber { get; set; }
}
public class Rating
{
[Key]
public int LookupId { get; set; }
public string LookupValue { get; set; }
}
the next step would be to define a view model that will represent the structure you want to serialize:
下一步是定义一个表示要序列化的结构的视图模型:
public class QuestionViewModel
{
public int CurriculumID { get; set; }
public string SubjectArea { get; set; }
public int CourseNumber { get; set; }
public IList<RatingViewModel> Answers { get; set; }
}
public class RatingViewModel
{
public int LookupId { get; set; }
public string LookupValue { get; set; }
}
and then in your controller you could fetch the entities from your database and populate the view model from them:
然后在你的控制器中,你可以从你的数据库中获取实体并从它们中填充视图模型:
public class QuestionsController: ApiController
{
[HttpGet]
[Route("api/questions")]
public IHttpActionResult Get()
{
using (var db = new MyDbContext())
{
IList<Rating> ratings = db.Ratings.ToList();
IList<Question> questions = db.Questions.ToList();
IList<QuestionViewModel> result = questions.Select(q => new QuestionViewModel
{
CurriculumID = q.CurriculumID,
SubjectArea = q.SubjectArea,
CourseNumber = q.CourseNumber,
Answers = ratings.Select(r => new RatingViewModel
{
LookupId = r.LookupId,
LookupValue = r.LookupValue,
}).ToList(),
}).ToList();
return this.Ok(result);
}
}
}
#1
1
Let's suppose that you have modeled your SQL tables with the following entities:
假设您已经用以下实体为SQL表建模:
public class Question
{
[Key]
public int CurriculumID { get; set; }
public string SubjectArea { get; set; }
public int CourseNumber { get; set; }
}
public class Rating
{
[Key]
public int LookupId { get; set; }
public string LookupValue { get; set; }
}
the next step would be to define a view model that will represent the structure you want to serialize:
下一步是定义一个表示要序列化的结构的视图模型:
public class QuestionViewModel
{
public int CurriculumID { get; set; }
public string SubjectArea { get; set; }
public int CourseNumber { get; set; }
public IList<RatingViewModel> Answers { get; set; }
}
public class RatingViewModel
{
public int LookupId { get; set; }
public string LookupValue { get; set; }
}
and then in your controller you could fetch the entities from your database and populate the view model from them:
然后在你的控制器中,你可以从你的数据库中获取实体并从它们中填充视图模型:
public class QuestionsController: ApiController
{
[HttpGet]
[Route("api/questions")]
public IHttpActionResult Get()
{
using (var db = new MyDbContext())
{
IList<Rating> ratings = db.Ratings.ToList();
IList<Question> questions = db.Questions.ToList();
IList<QuestionViewModel> result = questions.Select(q => new QuestionViewModel
{
CurriculumID = q.CurriculumID,
SubjectArea = q.SubjectArea,
CourseNumber = q.CourseNumber,
Answers = ratings.Select(r => new RatingViewModel
{
LookupId = r.LookupId,
LookupValue = r.LookupValue,
}).ToList(),
}).ToList();
return this.Ok(result);
}
}
}