1

I know that there are a lot similar questions here, but I did not find anyone which is asking exactly the same thing.

I am developing a project using ASP.NET and MVC4. I am using aspx as a view engine. I have a javascript code at my views, which gets as input a table of values, and creates a graph based on them.

I get those values from my database using the query below

        var query = from b in db.SchoolTestsPerModulePerStudent
                    where b.StudentID.Equals(2)
                    select b;

Now, my question is if I have these results in arrays (I did not figured out yet, how given b, to split its results in two arrays, one containing the id of test and the other containing the grade of the student yet, but I hope I will soon), what is the best(easy and clear) way to return these arrays from C# to javascript?

Jim Blum
  • 2,656
  • 5
  • 28
  • 37
  • the short answer is to serialize as json – Joel Coehoorn Jan 22 '14 at 22:34
  • @RobertKoritnik No. Just plain javascript, and my MVC C# codes – Jim Blum Jan 22 '14 at 23:10
  • So you're not allowed to call `return Json(query.ToList())` in your controller action? Because that would return your data as an array directly to your client side script. But you'd have to make an Ajax request (i.e. using jQuery) in order to avoid additional manual manipulation and processing on the client. – Robert Koritnik Jan 22 '14 at 23:40

2 Answers2

1

Serialise the object to JSON. If your Model is, for example, IEnumerable<string>:

Controller

public ActionResult Index()
{
    IList<string> strings = new List<string>();
    strings.Add("Python");
    strings.Add("C#");
    strings.Add("Javascript");
    strings.Add("Ruby");
    return View(strings);
}

View

@Json.Encode(Model)

Result:

["Python","C#","Javascript","Ruby"]

Extracting arrays

how given b, to split its results in two arrays

Use Linq.

var student = db.SchoolTestsPerModulePerStudent.Where(x => x.StudentId == 2);
var dates = student.Select(x => x.TestDate);
var grades = student.Select(x => x.TestGrade);
Community
  • 1
  • 1
Rowan Freeman
  • 15,724
  • 11
  • 69
  • 100
  • Thanks a lot! and how do I use that? I mean if my javascript has a code (test dates) xAxisPoints = [.....] and (student grades) yAxisPoints = [....] How do I use that json? Btw as I said, I have my model, but at the chart, I need to have one table containing only the dates of my model, and the other table to contain only the results at the tests the students gave that date... So how I will do that with the way you mentioned? – Jim Blum Jan 22 '14 at 22:46
  • hmmm.. your updated answer is much more helpful. If my model for example is (StudentID, ModuleID, TestDate, TestGrade), and I want to return a table having only the TestDates, and the other one Having only the TestGrades, how I can do that? – Jim Blum Jan 22 '14 at 22:51
  • Do you have an array of students, or does each student have an array of TestDates & TestGrades? – Rowan Freeman Jan 22 '14 at 22:58
  • this specific model that I mentioned above, has those four values mentioned above. Yes, I have other models for the student, and other for the grade, but this specific has those four values... And I want to return for a specific student, at a specific module, one array with the testDates and another one with their corresponding TestGrades – Jim Blum Jan 22 '14 at 23:09
  • So, given a student (b), how do you split an array? What are you splitting? I don't know what types `TestDate` and `TestGrade` are. – Rowan Freeman Jan 22 '14 at 23:11
  • TestDate is datetime, while test grade is an integer. and I want the ith element of the TestResult array, to be the corresponging result of the test that was taken at the ith element of the TestDate array. I want two different tables, given all the dates and results of a student – Jim Blum Jan 23 '14 at 02:10
  • Btw about the linq. the lines above do not execute two different queries? Because If is that way, I cannot use it, as the ith element of the grades could not correspond at the ith elelemtn of the dates... – Jim Blum Jan 23 '14 at 02:39
0

The model (viewmodel) that gets fed to your view should contain a strongly typed list containing an id and the grade. In your view (at least with razor), there are html helpers that build dropdown lists for you when you pass Model.MyList to it for example.

Sorry I can't give you an example yet, I'm not at work.

PmanAce
  • 4,000
  • 2
  • 24
  • 29