I'm having a little issue with an Edit view on a ViewModel. When I post my Edit view to the server for the first time, it needs to return the Edit view again with the same ViewModel where the database ID has been appended to.
This is the Edit method in the appropriate controller:
[HttpPost]
public ActionResult Edit(InvoiceDetailsViewModel invoice) {
using (var context = new HyperContext(WebSecurity.CurrentUserId)) {
if (ModelState.IsValid) {
if (invoice.ID == 0) {
var dbItem = Mapper.Map<eu.ecmt.RecruitmentDatabase.Models.Invoice>(invoice);
context.Invoices.Add(dbItem);
context.SaveChanges();
var newInvoice = Mapper.Map<InvoiceDetailsViewModel>(dbItem);
FillViewBag(context, newInvoice);
newInvoice.Description = "TEST";
return PartialView(newInvoice);
}
else {
context.Entry(Mapper.Map<eu.ecmt.RecruitmentDatabase.Models.Invoice>(invoice)).State = System.Data.EntityState.Modified;
context.SaveChanges();
return Content(Boolean.TrueString);
}
}
FillViewBag(context, invoice);
return PartialView(invoice);
}
}
The relevant part here is where the invoice.ID
is 0, the invoice is saved to the DB to get an ID and returned to the Edit view.
In that view I got these lines for starters:
@model eu.ecmt.RecruitmentDatabase.ViewModels.InvoiceDetailsViewModel
@using (Html.BeginForm("Edit", "Invoice", FormMethod.Post, new { id = "invoices-edit-form" })) {
@Html.ValidationSummary(true)
<script type="text/javascript">
$(document).ready(function () {
//$("#tabs").tabs();
InitProfileUI();
});
</script>
if (Model.ID != 0) {
<script type="text/javascript">
$(document).ready(function () {
LoadList('/InvoiceDetail/List/@Model.ID', '', 'invoice-details');
});
</script>
}
<fieldset>
<legend>Edit contract</legend>
@Html.HiddenFor(m => m.ID)
@Html.HiddenFor(m => m.InvoiceNumber)
@Html.HiddenFor(m => m.Created)
@Html.HiddenFor(m => m.CreatedBy)
@Html.HiddenFor(m => m.Modified)
@Html.HiddenFor(m => m.ModifiedBy)
When first rendering this view, the script element containing the LoadList
call is not in the output. When the form is posted and the view is rendered with the updated viewmodel, that element is in the output. The hidden field containing the ID of the invoice, though, still shows 0. So, in essence, what is happening here, is that the Model object in the ViewData dictionary is the correct version, the object that is being used in the expressions seems to be another, older, version.
Anyone care to explain this and point me into the right direction?