Running into some trouble with multiple forms on a single view.
Suppose I have the following viewmodel:
public class ChangeBankAccountViewModel
{
public IEnumerable<BankInfo> BankInfos { get; set; }
}
public class BankInfo
{
[Required]
public string BankAccount { get; set; }
public long Id { get; set; }
}
In my viewmodel, I want all BankInfos to be displayed underneath eachother, inside separate forms for each.
To achieve this, I'm using a partial view _EditBankInfo:
@model BankInfo
@using (Html.BeginForm())
{
@Html.HiddenFor(m => m.InvoiceStructureId)
@Html.TextBoxFor(m => m.IBANAccount)
<button type="submit">Update this stuff</button>
}
As well as my actual view BankInfo:
foreach(var info in Model.BankInfos)
{
Html.RenderPartial("_EditBankInfo", info);
}
Last, here are my 2 Action Methods:
[HttpGet]
public ActionResult BankInfo()
{
return View(new ChangeBankAccountViewModel{BankInfos = new [] {new BankInfo...});
}
[HttpPost]
public ActionResult BankInfo(BankInfo model)
{
if(ModelState.IsValid)
ModelState.Clear();
return BankInfo();
}
All of this is working hunky dory: Validation works smooth, posted model gets recognized and validated correctly... However, when the page reloads is when the problem arises. Because I'm using the same form multiple times, my ModelState will be applied multiple times. So when performing an update on one form, the next page load all of them will have the posted values.
Is there any way to easily prevent this from happening?
I've tried doing it without the partial views, but that screws up the naming a bit (they're unique, but serverside modelbinding won't recognize them).
Thanks for any answers.