Calling SubmitChanges on DataContext does not update database.

Posted by drasto on Stack Overflow See other posts from Stack Overflow or by drasto
Published on 2010-05-25T11:36:06Z Indexed on 2010/05/25 11:41 UTC
Read the original article Hit count: 427

Filed under:
|
|
|

In C# ASP.NET MVC application I use Link to SQL to provide data for my application. I have got simple database schema like this: My database

In my controller class I reference this data context called Model (as you can see on the right side of picture in properties) like this:

private Model model = new Model();

I've got a table (List) of Series rendered on my page. It renders properly and I was able to add delete functionality to delete Series like this:

public ActionResult Delete(int id) {
    model.Series.DeleteOnSubmit(model.Series.SingleOrDefault(s => s.ID == id));
    model.SubmitChanges();
    return RedirectToAction("Index");
}

Where appropriate action link looks like this:

<%: Html.ActionLink("Delete", "Delete", new { id=item.ID })%>

Also create (implemented in similar way) works fine. However edit does not work. My edit looks like this:

public ActionResult Edit(int id) {
    return View(model.Series.SingleOrDefault(s => s.ID == id));
}

[HttpPost]
public ActionResult Edit(Series series) {

    if (ModelState.IsValid) {
        UpdateModel(series);

        series.Title = series.Title + " some string to ensure title has changed";
        model.SubmitChanges();

        return RedirectToAction("Index");
}

I have controlled that my database has a primary key set up correctly. I debugged my application and found out that everything works as expected until the line with model.SubmitChanges();. This command does not apply the changes of Title property(or any other) against the database.

Please help.

© Stack Overflow or respective owner

Related posts about c#

Related posts about asp.net-mvc