I have the following model, view and controller.
Model
public class Person {
public string Name;// { get; set; }
public string Occupation { get; set; }
public int Salary { get; set; }
public int[] NumArr { get; set; }
}
View
<input type="button" id="Test" value="Test" />
@section Javascript{
<script type="text/javascript">
$(function () {
$("#Test").click(function () {
var data = { Name: "Michael Tom", Occupation: "Programmer", Salary: 8500, NumArr: [2,6,4,9] };
var url = "Home/GetJson";
$.ajax({
url: url,
dataType: "json",
type: "POST",
data: data,
traditional: true,
success: function (result) {
}
});
});
});
</script>
}
Controller
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public JsonResult GetJson(Person person) {
return Json(......);
}
}
Based on those code above, I like to ask three questions.
If we use public field instead of properties, the serializer doesn't serialize the json object to C# model so I always get null for "Name" field in controller. Why does it happen like that?
If I changed the type of NumArr property to List then it doesn't work. How can we use List instead of the int[]? I know I'm passing the array from JS. Can we pass List from JS as well?
I'm using "traditional: true" in Javascript code block of View because serialization doesn't work with "traditional: false". I heard that jQuery has three version of Json serializer. ASP.NET MVC's serializer supports only old version. Is it true?
3.1. If it's true, I like to know when you are going to get the latest version of MVC's serializer that supports jQuery's latest version.
3.2. Is there any way to register a custom Javascript Serializer just like we can register custom view engine? My friend suggested me that I can register custom value provider or custom model binder and use custom JS serializer in my custom value provider /model binder.
Thanks in advance. Please feel free to let me know if you are not clear about my questions. Thanks!