Mike Jonson

Mike Jonson

  • NA
  • 239
  • 193.6k

Problem with updating data in DB asp.net mvc

Jul 14 2012 5:31 PM

For page editing in Admin's Panel to update product data Write next method for this:

[HttpPost]
       
public ActionResult Edit(Product product)
       
{
           
if (ModelState.IsValid)
           
{
                repository
.SaveProduct(product);
               
TempData["message"] = string.Format("{0} have been saved", product.Name);
               
return RedirectToAction("Index");
           
}
           
else
           
{
               
return View(product);
           
}

       
}



and

public void SaveProduct(Product product)
       
{
           
if (product.ProductID == 0)
           
{
                context
.Products.Add(product);
           
}
            context
.SaveChanges();
       
}

And this view Edit:

@model SportStore.Domain.Entities.Product

@{
    ViewBag.Title = "Admin: Edit " + @Model.Name;
    Layout = "~/Views/Shared/_AdminLayout.cshtml";
}

<h1>Edit @Model.Name</h1>

@using (Html.BeginForm())
{
    @Html.EditorForModel()
   
<input type="submit" value = "Save" />
    @Html.ActionLink("Cancel and return to List", "Index")
}

When i click Save data are not updated. I try debug for public ActionResult Edit(Product product) and parametr product all correct. In method public void SaveProduct(Product product) the parameter is passed also correctly


Answers (1)