Hello :)
Using MVC 2 I have been trying to make this record store project. Creating records work but updating them doesn't. No exceptions are thrown either.
I examined getchangeset() right before submitchanges() it shows all zeros.
Thanks for your reading and you help :)
The Edit view:
<% using (Html.BeginForm("Edit", "Song")) { %>
<fieldset>
<%: Html.HiddenFor(model => model.SongID) %>
<%: Html.HiddenFor(model => model.DownloadCount) %>
<%: Html.HiddenFor(model => model.Rating) %>
<%: Html.HiddenFor(model => model.TagMapID) %>
<div class="editor-label">
<%= Html.LabelFor(model => model.AlbumID) %>
</div>
<div class="editor-field">
<%= Html.DropDownListFor(model => model.AlbumID, DataHelper.getAlbumList(Model.AlbumID)) %>
<%= Html.ValidationMessageFor(model => model.AlbumID) %>
</div>
<div class="editor-label">
<%= Html.LabelFor(model => model.FullName) %>
</div>
<div class="editor-field">
<%= Html.TextBoxFor(model => model.FullName)%>
<%= Html.ValidationMessageFor(model => model.FullName) %>
and so on...
Edit function
public ActionResult Edit(int id)
{
try
{
var model = songRepository.Song.Single(rec => rec.SongID == id);
return View(model);
}
catch (Exception anyEx)
{
throw anyEx;
//return View("Error")
}
}
Edit post function
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(Song model)
{
try
{
if (ModelState.IsValid)
{
songRepository.SaveSong(model);
TempData["adminmsg"] = "Song saved";
return RedirectToAction("List", "Song");
}
else
{
TempData["adminmsg"] = "Song not saved";
return View("Edit", model);
}
}
catch (Exception anyEx)
{
throw anyEx;
}
}
This is what saveSong() looks like in SqlSongRepository
public void SaveSong(Song song)
{
if (song.SongID == 0)
{
songTable.InsertOnSubmit(song);
}
else
{
songTable.Attach(song);
songTable.Context.Refresh(RefreshMode.KeepChanges, song);
}
songTable.Context.SubmitChanges();
}
[Context things]
This is the context repository
public class ContextRepository : IContextRepository
{
private string connStr;
public ContextRepository(string connectionString)
{
this.connStr = connectionString;
}
public DataContext MasterContext
{
get { return new DataContext(connectionString); }
}
public string connectionString
{
get { return connStr; }
}
}
In the song controller for example
public SongController(IContextRepository contextRepository)
{
this.contextRepository = contextRepository;
MasterContext = this.contextRepository.MasterContext;
DataHelper.InitContext(contextRepository);
songRepository = new SqlSongRepository(contextRepository.connectionString);
}
Then the songRepository is used as I posted before.