I'm using dotconnect for postgrsql with entity framework. I have a view that displays multiple rows from a table. The view loops through the model and displays rows as below...
Code: Select all
@model IEnumerable<IntegratorModel.ServiceOrderAttribute>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@Html.ActionLink("Update rows", "Edit")
<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>ServiceOrderAttribute</legend>
@foreach (var item in Model)
{
<div class="editor-label">
@Html.LabelFor(model => item.AttributeName)
</div>
<div class="editor-field">
@Html.EditorFor(model => item.AttributeName)
@Html.ValidationMessageFor(model => item.AttributeName)
</div>
<div class="editor-label">
@Html.LabelFor(model => item.AttributeValue)
</div>
<div class="editor-field">
@Html.EditorFor(model => item.AttributeValue)
@Html.ValidationMessageFor(model => item.AttributeValue)
</div>
}
<input type="submit" value="Edit" />
</fieldset>
}
How do i pass the multiple rows updated to the controller and have them update the database table. My controller is below (as you can see it can handle just one row)
Code: Select all
[HttpPost]
public ActionResult Edit(ServiceOrderAttribute serviceorderattribute)
{
if (ModelState.IsValid)
{
db.ServiceOrderAttributes.Attach(serviceorderattribute);
db.ObjectStateManager.ChangeObjectState(serviceorderattribute, EntityState.Modified);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(serviceorderattribute);
}
I also tried passing in IEnumerable (using html form collection) to the controller, however on submitting it throws an error
Code: Select all
[InvalidOperationException: An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.]
System.Data.Objects.ObjectContext.VerifyRootForAdd(Boolean doAttach, String entitySetName, IEntityWrapper wrappedEntity, EntityEntry existingEntry, EntitySet& entitySet, Boolean& isNoOperation) +8595812
System.Data.Objects.ObjectContext.AttachTo(String entitySetName, Object entity) +194
System.Data.Objects.ObjectSet`1.Attach(TEntity entity) +29
Code: Select all
[HttpPost]
public ActionResult Edit(IEnumerable<ServiceOrderAttribute> serviceOrderAttribute)
{
foreach (var item in serviceOrderAttribute)
{
if (ModelState.IsValid)
{
db.ServiceOrderAttributes.Attach(item);
db.ObjectStateManager.ChangeObjectState(item, EntityState.Modified);
db.SaveChanges();
// return RedirectToAction("Index");
}
}
return View();
}
Code in view:
@foreach (var item in Model)
{
using (Html.BeginCollectionItem("ServiceOrderAttribute"))
{
Html.HiddenFor(model => item.Id);
Html.HiddenFor(model => item.ServiceOrderId);
<div class="editor-label">
@Html.LabelFor(model =>item.AttributeName)
</div>
<div class="editor-field">
@Html.EditorFor(model => item.AttributeName)
@Html.ValidationMessageFor(model => item.AttributeName)
</div>
<div class="editor-label">
@Html.LabelFor(model => item.AttributeValue)
</div>
<div class="editor-field">
@Html.EditorFor(model => item.AttributeValue)
@Html.ValidationMessageFor(model => item.AttributeValue)
</div>
}