Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6704525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:17:56+00:00 2026-05-26T07:17:56+00:00

I’m building a simple MVC Movie Application, using a repository pattern and Class Library

  • 0

I’m building a simple MVC Movie Application, using a repository pattern and Class Library for my Linq to SQL Classes. I can’t seem to get my objects to UPDATE back to the database.. I’m missing something now sure what it is:

    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");
}

View

<% 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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T07:17:56+00:00Added an answer on May 26, 2026 at 7:17 am

    In your method EditMovie, the object movie that you receive as the argument, is not actually a database-bound object. It gets constructed for you by MVC runtime, and your DataContext has no knowledge of it. Therefore, when you call Update(), the DataContext doesn’t see any changes to write to the database.

    What you should do instead is find this object in the database, then copy all fields from the method’s argument into it, and then call Update(). Like so:

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult EditMovie(Movie movie)
    {
        if (!ModelState.IsValid)
            return View(movie);
    
        var existingMovie = _movieRepository.FetchById( movie.Id );
        existingMovie.Title = movie.Title;
        _movieRepository.Update();
    
        return RedirectToAction("Index");
    }
    

    For this to work, you also have to include your Movie’s ID in your form (as a hidden field), so that it may be posted back by the browser, and thus enable you to distinguish update to one movie from update to another. Like so:

        <legend>Details</legend>
        <p>
            <label for="Title">Title:</label><br/>
                <%= Html.TextBox("Title", Model.Title) %>
                <%= Html.ValidationMessage("Title", "*") %>
                <%= Html.HiddenFor( m => m.Id ) %>   //<------
        </p>
    

    EDIT: As Mystere Man pointed out, you do not need to add this hidden field if your URL contains the Id.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.