Calling the SubmitChanges method on a DataContext does not update the database

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

In my controller class, I refer to this data context, which is called Model(as you can see in the properties on the right side of the image), for example:

private Model model = new Model();

I have a table (List) of Series displayed on my page. It displays correctly, and I was able to add a delete function to remove the series as follows:

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

If necessary, the link to the action is as follows:

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

Also create (implemented in a similar way) works fine. However, editing 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");
    }
    return View(series);
}

, . , , , model.SubmitChanges();. Title ( ) .

.

EDIT: : model.Series.Attach(series); model.SubmitChanges(); - - . , Edit , model.

EDIT: , Edit:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" 
Inherits="System.Web.Mvc.ViewPage<TVSeriesInfoApp.Models.Series>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Edit
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<h2>Edit</h2>

<% using (Html.BeginForm()) {%>
    <%: Html.ValidationSummary(true) %>

    <fieldset>
        <legend>Fields</legend>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.Title) %>
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.Title) %>
            <%: Html.ValidationMessageFor(model => model.Title) %>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.Seasons) %>
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.Seasons) %>
            <%: Html.ValidationMessageFor(model => model.Seasons) %>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.Stars) %>
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.Stars) %>
            <%: Html.ValidationMessageFor(model => model.Stars) %>
        </div>

        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>

<% } %>

<div>
    <%: Html.ActionLink("Back to List", "Index") %>
</div>

</asp:Content>
+3
2

Edit ( ):

[HttpPost]
public ActionResult Edit(int id, Series series)
{
    Series updatingSeries = model.Series.Single(s => s.ID == id);

    try
    {
        TryUpdateModel(updatingSeries);
        model.SubmitChanges();

        return RedirectToAction("Details", new { id = updatingSeries.ID });
    }
    catch
    {
        return View(updatingSeries);
    }
}

, ModelState . - ? ?

+4

-, "" HTTP GET ( , Html.ActionLink("Delete", "Delete", new { id=item.ID }).

, Attach DataContext.

+1

Source: https://habr.com/ru/post/1747008/


All Articles