2

I am new to angularjs and ASP.NET webapi. I am working on some requirement which my base tables are as below

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(...)

I need the resultant JSON data like as below

    $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

So how can write the two methods getData(to generate JSon data). please can anyone help me to serialize this structure in ASP.NET

https://plnkr.co/edit/73oA3rsrre8gqYX9V25W?p=preview

0

1 Answer 1

1

Let's suppose that you have modeled your SQL tables with the following entities:

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);
        }
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you very much for the quick response. I followed your procedure but i got an error like as below. Type '<>f__AnonymousType54[System.String,System.String,System.String,System.Collections.Generic.List1[<>f__AnonymousType6`2[System.Int32,System.String]]]' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for other supported
[Table("Course")] public class Course { [Key] public string CurriculumID { get; set; } public string SubjectArea { get; set; } public string CourseNumber { get; set; } } [Table("sp.option_lookup_st_fdbk")] public class option_lookup_st_fdbk { [Key] public int lookupId { get; set; } public string LookupValue { get; set; } }
and in the method IList<option_lookup_st_fdbk> rating = db.option_lookup_st_fdbk.ToList(); IList<Course> questions = db.Course.ToList(); var result = questions.Select(q => new { CurriculumID = q.CurriculumID, SubjectArea = q.SubjectArea, CourseNumber = q.CourseNumber, answers = rating.Select(r => new { lookupId = r.lookupId, LookupValue = r.LookupValue }).ToList(), }).ToList(); return this.Ok(result);
It looks like anonymous objects cannot be properly serialized in the Web API. This is a bad practice anyway. I have updated my answer to show how you could project your entities to a view model which can then be returned.
Thank you very much.. it is working now. I would like to know what is wrong with the earlier code. Can you please explain if poosible
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.