I have this json array-ish variable which gets values from a table:
var data = { "Model": [] };
$(".student-grade-data-row").each(function () {
data.Model.push({
'subjectId' : currentSubjectId,
'studentId': $(this).attr('id'),
'grade1' : $(this).find(".grade-select").val(),
'mult' : $(this).find(".mult-select").val(),
'subject': $(this).find(".topic-input").val(),
});
});
It was made to resemble a MVC class called grade
public partial class grade
{
public int id { get; set; } //auto-set
public int subjectId { get; set; }
public string studentId { get; set; }
public int grade1 { get; set; }
public int mult { get; set; }
public string subject { get; set; }
public System.DateTime date { get; set; } //auto-set
}
Now in the perfect world, I would like to have the values inside the data variable to be send into my controller like this:
$.ajax({
url: '/Ajax/SendGrades',
data: JSON.stringify(data),
contentType: "application/json; charset=utf-8",
success: function (dt) {
console.log(dt);
}
});
And there I could pick it up, and serialize it as a proper object list.
The problem isI cannot even get a glimpse of what really am I sending to the controller and how possibly could I link it to the object.
I tried making the function take an agrument of type List<grade> Model, but It would keep telling me the value is null.
I tried using some basic serialization but cant get it to work either. The code
public ActionResult SendGrades(JsonResult Model)
{
List<grade> g = new List<grade>();
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(grade));
g = (List<grade>)serializer.ReadObject(Model);
return Content("Hi");
}
Gives me an error saying
Cannot convert from System.Web.Mvc.JsonResult to System.IO.Stream
What would be the proper way to do this so I could later safely store the values in my database?
I'm fairly new to this type of work so I apologize for any noob-ism. Any help would be great here.
public ActionResult SendGrades(List<grade> Model)(and always useurl: 'Url.Action("SendGrades", "Ajax")',to generate the correct url). You also need to setcontentType: "application/json; charset=utf-8",Url.Action?contentTypeoption)?var url = '@Url.Action(..)';to create a global variable in the main view, or use adata-url="@Url.Action(..)"to assign it in the element that triggers the ajax