How to pass a list of objects instead of one object to a POST action method

john Gu picture john Gu · Apr 30, 2012 · Viewed 7.7k times · Source

I have the following GET and POST action methods:-

public ActionResult Create(int visitid)
{
    VisitLabResult vlr = new VisitLabResult();
    vlr.DateTaken = DateTime.Now;
    ViewBag.LabTestID = new SelectList(repository.FindAllLabTest(), "LabTestID", "Description");

    return View();
} 

//
// POST: /VisitLabResult/Create

[HttpPost]
public ActionResult Create(VisitLabResult visitlabresult, int visitid)
{
    try
    {
        if (ModelState.IsValid)
        {
            visitlabresult.VisitID = visitid;
            repository.AddVisitLabResult(visitlabresult);
            repository.Save();
            return RedirectToAction("Edit", "Visit", new { id = visitid });
        }
    }
    catch (DbUpdateException) {

        ModelState.AddModelError(string.Empty, "The Same test Type might have been already created,, go back to the Visit page to see the avilalbe Lab Tests");

    }
    ViewBag.LabTestID = new SelectList(repository.FindAllLabTest(), "LabTestID", "Description", visitlabresult.LabTestID);

    return View(visitlabresult);
}

Currently the view display the associated fields to create only one object,, but how i can define list of objects instead of one object to be able to quickly add for example 10 objects at the same “Create” request. My Create view look like:-

@model Medical.Models.VisitLabResult

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

@section scripts{
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
}
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>VisitLabResult</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.LabTestID, "LabTest")
        </div>
        <div class="editor-field">
            @Html.DropDownList("LabTestID", String.Empty)

Answer

Pravin Pawar picture Pravin Pawar · May 1, 2012

Your viewModel

public class LabResult
{
    public int ResultId { get; set; }
    public string Name { get; set; }
    //rest of the properties
}

Your controller

public class LabController : Controller
{
    //
    // GET: /Lab/ns

    public ActionResult Index()
    {
        var lst = new List<LabResult>();
        lst.Add(new LabResult() { Name = "Pravin", ResultId = 1 });
        lst.Add(new LabResult() { Name = "Pradeep", ResultId = 2 });

        return View(lst);
    }

    [HttpPost]
    public ActionResult EditAll(ICollection<LabResult> results)
    {
        //savr results here
        return RedirectToAction("Index");
    }

}

Your view

@model IList<MvcApplication2.Models.LabResult>
@using (Html.BeginForm("EditAll", "Lab", FormMethod.Post))
{
    <table>
        <tr>
            <th>
                ResultId
            </th>
            <th>
                Name
            </th>
        </tr>
        @for (int item = 0; item < Model.Count(); item++)
        {
            <tr>
                <td>
                    @Html.TextBoxFor(modelItem => Model[item].ResultId)
                </td>
                <td>
                    @Html.TextBoxFor(modelItem => Model[item].Name)
                </td>
            </tr>
        }
    </table>
    <input type="submit" value="Edit All" />
}

Your view will be rendered as follows, this array based naming convention makes it possible for Defaultbinder to convert it into ICollection as a first parameter of action EditAll

<tr>
<td>
        <input name="[0].ResultId" type="text" value="1" />
    </td>
    <td>
        <input name="[0].Name" type="text" value="Pravin" />
    </td>
    </tr>
    <tr>
    <td>
        <input name="[1].ResultId" type="text" value="2" />
    </td>
    <td>
        <input name="[1].Name" type="text" value="Pradeep" />
    </td>
    </tr>