Search code examples
.netasp.net-mvcrazor-2

Handle modification in a nested List in the Model in ASP .Net MVC


I'd like to have a form which related to a object + a list of object with property related

ex : Name and for each x items : Active or not / Price

Note : I know the number of items, it is related to another table. All the data can be edited by the user.

How can I achieve this with ASP .Net MVC ?

public class AddChargeModel
{
    [Required]
    [Display(Name = "Name")]
    public string Nom { get; set; }
    [Required]
    public List<ChargeLotModel> ChargeLot;
}

public class ChargeLotModel
{
    [Required]
    [Display(Name = "IdLot")]
    public int IdLot { get; set; }
    [Display(Name = "Price")]
    public decimal Price { get; set; }
    [Display(Name = "Active")]
    public bool Active { get; set; }
}

I associate the class AddChargeModel with my view :

@model Models.AddChargeModel
@using (Html.BeginForm())
{
  <label>@Html.LabelFor(m => m.Name)</label>
  @Html.EditorFor(m => m.Name)
  @Html.ValidationMessageFor(m => m.Name)

  <table>
   <tr>
    <th>Price</th>
    <th>Active</th>
   </tr>
   @for (var lotindex = 0; lotindex < ViewData.Model.ChargeLot.Count(); lotindex++)
   {
    <tr>
     <td>@Html.EditorFor(model => model.ChargeLot[lotindex].Price) @Html.HiddenFor(model => model.ChargeLot[lotindex].IdLot)</td>
     <td>@Html.EditorFor(model => model.ChargeLot[lotindex].Active)</td>
    </tr>
    }
   </table>
   <input type="submit" value="Valider" class="button" />
}

When I click on the button, then I enter in the controller function :

[HttpPost]
public ActionResult Add(AddChargeModel model)
{
   ...
}

model.Name is filled but not model.ChargeLot == null.

The controls in the array are named like ChargeLot_0__Price on the web page. (and it should work if I well understood)

Do you have the solution to make it work ?


Solution

  • Your ChargeLot "property" has no getter or setter, so the model binder can't fill it with the posted data. It's just a standard instance variable rather than a property, and nothing on your model ever sets it. You need instead:

    public List<ChargeLotModel> ChargeLot { get; set; }