vendredi 31 juillet 2015

Saving Multiple Entries of an Entity in MVC with Entity Framework

I am trying to have a view where I can update multiple entries of the same entity type simultaneously. The data all come from a I have the following setup, but it won't save the entries properly.

View

@model List<WebApplication1.Models.A>


@using (Html.BeginForm("updateInlinePost", "Default", "POST"))
{
    <table class="table">
        <tr>Headings</tr>
        @for (int i = 0; i < Model.Count(); i++)
        {
        <tr>
            @Html.EditorFor(m => Model[i], "EditorTemplate")
        </tr>
        }
        </table>

    <input type="submit" value="Save" class="btn btn-default" />

Editor Template

@model WebApplication1.Models.A

            <td>@Html.DropDownListFor(model => model.Status, new SelectList(new List<String> { "Open", "Resolved", "Cancelled" }, "Value"), Model.Status)
            </td>             
            <td>
                @Html.EditorFor(model => model.Revised_Estimate, new { style = "width:5px" })
            </td>
            <td>
                @Html.EditorFor(model => model.Actual_Completion_Date, new { htmlAttributes = new { @Value = Model.Actual_Completion_Date, @class = "form-control" } })
            </td>

Controller

[HttpPost]
public ActionResult update(List<A> aList)
{
    for (int i = 0; i < aList.Count(); i++ )
    {
        A entry = aList.ElementAt(i);
        db.Save(entry);  // Built-in function to save each entry
        db.SaveChanges();
    }
    return RedirectToAction();
}

Any pointers why this is not saving properly?

Aucun commentaire:

Enregistrer un commentaire