my problem is that after click on button "Save", I get the model passed to controller from view is null.
Here my code of the View and Controller. Do you know where I am doing wrong? Thank you so much.
View Certificazioni.cshtml
@model List<ElencoCertificazioniItem>
...
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
...
<tbody>
@{
for (int i = 0; i < Model.Count; i++)
{
<tr>
<td>@Html.LabelFor(m => Model[i].Id) </td>
<td>@Html.LabelFor(m => Model[i].description)</td>
<td>@Html.EditorFor(m => Model[i].Field1.Value)</td>
<td>@Html.EditorFor(m => Model[i].Field2.Value)</td>
</tr>
}
}
</tbody>
}
....
}
Controller
[HttpPost]
[ValidateAntiForgeryToken]
[HttpParamAction]
public ActionResult SaveItems(List<ElencoCertificazioniItem> model)
{
//the items here is null!!! ;(
return saveItems(model);
}
Model
public class ElencoCertificazioniItem
{
public int Id { get; set; }
public string description { get; set; }
public bool? Field1 { get; set; }
public bool? Field2 { get; set; }
}
I use HttpParamAction to manage calls to different methods controller (I have 2 button in the same form).
public class HttpParamActionAttribute : ActionNameSelectorAttribute
{
public override bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo)
{
if (actionName.Equals(methodInfo.Name, StringComparison.InvariantCultureIgnoreCase))
return true;
var request = controllerContext.RequestContext.HttpContext.Request;
return request[methodInfo.Name] != null;
}
}
The list is not null and contains right count elements, but the items within are null and Id properties is always 0!
modelisnull, or its an empty collection, of that some properties of the items in the collection arenull? (you do not generate any form controls for propertiesidanddescription). And what is the[HttpParamAction]attribute?[HttpParamAction])? Maybe your collection is posted to the wrong action?