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:
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");
}
return View(series);
}
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.
EDIT:
If I add this line: model.Series.Attach(series);
just before model.SubmitChanges();
there is no change - edits still does not reflect to database. The instance passed to Edit method as a parameter is already attached to the data context model
.
EDIT: Code of view that belongs to method 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>