MVC 2 を使用して、このレコード ストア プロジェクトを作成しようとしています。レコードの作成は機能しますが、更新は機能しません。例外もスローされません。
submitchanges() の直前に getchangeset() を調べたところ、すべてゼロが表示されました。
読んでくれてありがとう、助けてください:)
編集ビュー:
<% 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...
編集機能
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")
}
}
投稿機能の編集
[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;
}
}
これは、SqlSongRepository で saveSong() がどのように見えるかです
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();
}
【文脈のこと】
これはコンテキストリポジトリです
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; }
}
}
たとえば、ソングコントローラーでは
public SongController(IContextRepository contextRepository)
{
this.contextRepository = contextRepository;
MasterContext = this.contextRepository.MasterContext;
DataHelper.InitContext(contextRepository);
songRepository = new SqlSongRepository(contextRepository.connectionString);
}
次に、前に投稿したように songRepository が使用されます。