I think I am missing something little.
All I want to do is to pass the List of models from View to Controller, I have 7 items and I succeeded to pass those 7 items but all its properties values are default.
This is my view:
@model List<News>
@using (Html.BeginForm("SaveNewsPositions", "home", new { area = "news" }, FormMethod.Post))
{
<a href="#" id="savePositions">Save positions</a>
<div class="news-items">
@foreach (var news in Model)
{
@Html.Partial("_NewsControl", news)
}
</div>
}
And the JS code:
$('#savePositions').on('click', function () {
var form = $('form');
$.post('@Url.Action("SaveNewsPositions", "home", new {area = "news"})', form.serialize(), function (response) {
if (response.success) {
//TODO...
}
});
When I debug the form.serialize() I see the object with all values, for example:
Position=1&Id=106 etc...
But when it comes to the Controller:
[HttpPost]
public JsonResult SaveNewsPositions(List<News> models)
{
//Some Code...
}
I see that models have 7 items and each item has Position with 0 value and Id that is identity (first item: id = 1, second id = 2, etc.).
How can I fix it?