Save Multiple Model in Single Page

Pattern ini saya gunakan ketika 1 page digunakan untuk menyimpan beberapa model. Tampilan page:

Capture

Secara konsep, ada 2 kelas yang digunakan, yaitu:

  1. [ModelName]Table.cs
  2. [ModelName]FormStub.cs

[ModelName]Table memiliki atribut List<FormStub>, dan bertugas untuk meng-inisialisasi elemen dari List tersebut. List<FormStub> dikirim ke View sebagai model, sedangkan Table dikirim melalui ViewBag (jika dibutuhkan).

Sequence diagram

asp pattern

Contoh kode di view

[code language=”csharp”]
@model List<ActionPlanProgressFormStub>

//…

@foreach (ActionPlanProgressPresentationStub item in table.ProgressList)
{
form = Model.Where(m => m.WeeklyRcaId == item.WeeklyRcaId).FirstOrDefault();
<tr>
<td>@item.ActionPlan</td>
<td>@item.PIC</td>
<td>@item.DueDateFullFormat</td>
<td>@item.LastProgress%</td>
<td>
<div class="form-group has-feedback">
@Html.HiddenFor(m => m[index].Id)
@Html.HiddenFor(m => m[index].WeeklyRcaId)
@Html.HiddenFor(m => m[index].WeeklyReportId)
@Html.EditorFor(m => m[index].Progress, new { htmlAttributes = new { @class = "form-control" } })
<span class="form-control-feedback">%</span>
</div>
</td>
</tr>
++index;
}
[/code]

Contoh kode di Controller (yang menerima post)

[code language=”csharp”]
public ActionResult EditActionPlan(int weeklyReportId, List<ActionPlanProgressFormStub> model)
{
if (ModelState.IsValid)
{
foreach (ActionPlanProgressFormStub item in model)
Repo.SaveActionPlanProgress(item.GetActionPlanProgress());
}
}
[/code]

Chandra Oemaryadi has written 244 articles

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>