0

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!

2
  • Show your model. And do your mean model is null, or its an empty collection, of that some properties of the items in the collection are null? (you do not generate any form controls for properties id and description). And what is the [HttpParamAction] attribute? Commented Apr 12, 2016 at 8:18
  • Do you have more than one submit button (because you use [HttpParamAction])? Maybe your collection is posted to the wrong action? Commented Apr 12, 2016 at 8:35

1 Answer 1

1

Your ID and description will always be 0 and null because you don't use any input to post them in form. If you want return them, you have to use input hiddent to pass this values

<tr>
    <td>
        @Html.LabelFor(m => Model[i].Id)
        @Html.HiddenFor(m => Model[i].Id)
    </td>
    <td>
        @Html.LabelFor(m => Model[i].description)
        @Html.HiddenFor(m => Model[i].description)
    </td>
    <td>@Html.EditorFor(m => Model[i].Field1)</td>
    <td>@Html.EditorFor(m => Model[i].Field2)</td>
</tr>
Sign up to request clarification or add additional context in comments.

2 Comments

Ok thank you...I Confirm Now Id and description are valued...very thanks
the answer is also that of Stephen Muecke ... "And since Field1 is a nullable bool, it needs to be @Html.EditorFor(m => Model[i].Field1) (no Value at the end) – Stephen Muecke" Thank you so much guys

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.