I have: VIEW
<script type="text/javascript">
function postData() {
var urlact = '@Url.Action("createDoc")';
var model = '@Html.Raw(Json.Encode(Model))';
alert(model);
$.ajax({
data: stringify(model),
type: "POST",
url: urlact,
datatype: "json",
contentType: "application/json; charset=utf-8",
success: function (result) {
window.location.href = '@Url.Action("CreateWordprocessingDocument","Movies")'
}
});
}
</script>
Controller
[HttpPost]
public void createDoc(string mov)
{
var movies = new JavaScriptSerializer().Deserialize<IEnumerable<Movie>>(mov);
//using movies...
}
Model
//[Serializable]
public class Movie
{
//Added Data Annotations for Title & Genre
public int ID { get; set; }
[Required(ErrorMessage = "Insert name")]
public string Title { get; set; }
[DataType(DataType.Date)]
public DateTime ReleaseDate { get; set; }
[Required(ErrorMessage = "Inserist genre")]
public string Genre { get; set; }
[DataType(DataType.Currency)]
public decimal Price { get; set; }
}
Why when I post the stringified data from View (through the Ajax post function) to Controller (createDoc) it stops throwing a ArgumentNullException (seems the Model passed is empty)? Any workaround/solution?
Note: without stringifying the model it all works, but I'm trying to stringify it cause the other way I've got some issues with the DateTime format.
Note/2: I've also tried replacing the string mov in the input parameters of the Controller action with IEnumerable movies, but it didn't work either.