What Is The Best(easy And Clear) Way To Return Arrays From C# To Javascript
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 a
Solution 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);
Solution 2:
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.
Post a Comment for "What Is The Best(easy And Clear) Way To Return Arrays From C# To Javascript"