저는 Linq to SQL 클래스를위한 저장소 패턴과 클래스 라이브러리를 사용하여 간단한 MVC Movie Application을 구축하고 있습니다. 나는 .. 나는 그것이 무엇인지 지금 확인 뭔가 누락 데이터베이스에 다시 업데이트하려면 내 개체를 얻을 수없는 것 :.NET 4.0 Linq to SQL - 객체 업데이트가 작동하지 않습니다.
public class MovieRepository : BaseRepository, IMovieRepository
{
/// <summary>
/// Updates the specified movie.
/// </summary>
public void Update()
{
GetDataContext.SubmitChanges();
}
/// <summary>
/// Fetches the by id.
/// </summary>
/// <param name="id">The id.</param>
public Movie FetchById(int id)
{
Movie movie = (from n in GetDataContext.Movies
where n.ID == id
select n).First();
return movie;
}
}
BaseRepository.cs을
public abstract class BaseRepository
{
private static VideoStoreDBDataContext _videoStoreDbDataContext;
protected static VideoStoreDBDataContext GetDataContext
{
get
{
if (_videoStoreDbDataContext == null)
{
_videoStoreDbDataContext = new VideoStoreDBDataContext();
}
return _videoStoreDbDataContext;
}
}
}
HomeController
public ActionResult EditMovie(int Id)
{
Movie movie = _movieRepository.FetchById(Id);
if (movie == null)
return RedirectToAction("Error", "Home");
return View(movie);
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult EditMovie(Movie movie)
{
if (!ModelState.IsValid)
return View(movie);
// NOTE: movie object does infact contain changes made using the VIEW.
_movieRepository.Update();
return RedirectToAction("Index");
}
보기
<% using (Html.BeginForm()) {%>
<fieldset>
<legend>Details</legend>
<p>
<label for="Title">Title:</label><br/>
<%= Html.TextBox("Title", Model.Title) %>
<%= Html.ValidationMessage("Title", "*") %>
</p>
<p>
<input type="submit" value="Update Movie" />
</p>
</fieldset>
<% } %>
<div>
<%=Html.ActionLink("Back to List", "Index") %>
</div>
사실, URL에서 id를 가져 오기 위해 post 매개 변수에 id 매개 변수를 추가하는 것만으로 숨길 필요가 없습니다. –
@Mystere : 매개 변수가 URL에 있으면 매개 변수를 메서드에 실제로 추가 할 필요가 없습니다. URL 매개 변수는 모델 객체에 자동으로 매핑됩니다. 그러나 게시글에는 어떤 라우팅 구성이 사용되는지는 명시되어 있지 않으며 분명히 OP의 낮은 경험을 고려할 때 여기서는 안전한 쪽을 선호합니다. –