T L
T L

Reputation: 514

Another null collection being passed to MVC Controller

I need additional eyes to see:

  1. What I am doing wrong as I try to pass a collection of objects to a MVC controller and all I get is sgList = null.
  2. How can I check so that I only save the rows that being changed.

    [HttpPost]
    public ActionResult Index(IList<EZone_ServiceGroup> sgList)
    {
        try
        {
            foreach (EZone_ServiceGroup sg in sgList)
                svcGroupRepo.UpdateServiceGroup(sg);
    
            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }
    

View:

@model IEnumerable<KTCEzone.Domain.Entities.EZone_ServiceGroup>
@{
    ViewBag.Title = "Index";
}
@using (Html.BeginForm())
{
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Save" class="btn btn-default" />
        </div>
    </div>
    <div class="row">
        <table class="table table-condensed table-bordered table-hover table-striped small" id="sgTable">
            <tr>
                <th class="col-sm-12">@Html.DisplayNameFor(model => model.GroupID)</th>
                <th>@Html.DisplayNameFor(model => model.GroupName)</th>
                <th>@Html.DisplayNameFor(model => model.ParentGroupID)</th>
                <th>@Html.DisplayNameFor(model => model.Active)</th>
                <th>@Html.DisplayNameFor(model => model.OrderIndex)</th>
            </tr>

            @{var items = Model.ToArray();}
            @for (int i = 0; i < items.Length; i++)
            {
                <tr>
                    <td>@Html.DisplayFor(modelItem => items[i].GroupID)</td>
                    <td>@Html.EditorFor(modelItem => items[i].GroupName) </td>
                    <td>@Html.EditorFor(modelItem => items[i].ParentGroupID) </td>
                    <td>@Html.CheckBoxFor(modelItem => items[i].Active) </td>
                    <td>@Html.EditorFor(modelItem => items[i].OrderIndex) </td>
                </tr>
            }
        </table>
    </div>
}

Model:

public class EZone_ServiceGroup
{
    public int GroupID { get; set; }
    public string GroupName { get; set; }
    public bool Active { get; set; }
    public int OrderIndex { get; set; }
    public int ParentGroupID { get; set; }
}

Upvotes: 0

Views: 62

Answers (1)

user3559349
user3559349

Reputation:

Change your model to @model IList<KTCEzone.Domain.Entities.EZone_ServiceGroup>, and remove @{var items = Model.ToArray();} from the view and use

@for (int i = 0; i < Model.Count; i++)
{
  <tr>
    <td>@Html.DisplayFor(m => m[i].GroupID)</td>
    <td>@Html.EditorFor(m=> m[i].GroupName)</td>
    <td>@Html.EditorFor(m=> m[i].ParentGroupID)</td>
    <td>@Html.CheckBoxFor(m=> m[i].Active) </td>
    <td>@Html.EditorFor(m=> m[i].OrderIndex) </td>
  </tr>
}

which will correctly name your elements. If you cannot change the collection to IList, then you need to use a custom EditorTemplate for the type of the model, and use in conjunction with @Html.EditorFor()

As for "How can I check so that I only save the rows that being changed", all controls will be posted back, so you need to compare the posted values with the original values in the controller.

Upvotes: 1

Related Questions