Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more: , +
Hello,

I am Creating MVC Application MvcMovies by guiding Asp.net tutorial(http://www.asp.net/mvc/tutorials/getting-started-with-aspnet-mvc3/cs/accessing-your-model's-data-from-a-controller[^])

I have created sql server database and created a table Movies,

my only question is that where to store data after called savechanges()

if you want code than,

Below is my code model controller and view,

Movies.cs

C#
public class Movies
    {
        public int id { get; set; }
        public string title { get; set; }
        public DateTime ReleaseDate { get; set; }
        public string Genre { get; set; }
        public decimal price { get; set; }
        public string Ratings { get; set; }

    }

    public class MoviesDBContext : DbContext
    {
        public DbSet<Movies> Movies { get; set; }
    }


MoviesController.cs

C#
//
       // GET: /Movies/Create

       public ActionResult Create()
       {
           return View();
       }

       //
       // POST: /Movies/Create

       [HttpPost]
       public ActionResult Create(Movies movies)
       {
           if (ModelState.IsValid)
           {
               db.Movies.Add(movies);
               db.SaveChanges();
               return RedirectToAction("Index");
           }

           return View(movies);
       }


create.cshtml
HTML
@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Movies</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.title)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.title)
            @Html.ValidationMessageFor(model => model.title)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.ReleaseDate)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ReleaseDate)
            @Html.ValidationMessageFor(model => model.ReleaseDate)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Genre)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Genre)
            @Html.ValidationMessageFor(model => model.Genre)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.price)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.price)
            @Html.ValidationMessageFor(model => model.price)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Ratings)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Ratings)
            @Html.ValidationMessageFor(model => model.Ratings)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}


I need to know that when I am creating new record where it will be inserted.

I have checked database table but there is no records.

Thanks.
Posted
Updated 15-Jan-13 5:58am
v2
Comments
Zoltán Zörgő 15-Jan-13 14:06pm    
What do you mean by "where to store data after called savechanges"?
Jameel VM 17-Jan-13 1:26am    
Please improve your question. What happened while saving?Any Exception?

1 solution

You are trying to save it without passing any value.
just modify you post method as

SQL
[HttpPost]
        public ActionResult Create(Movies movies)
        {
            if (ModelState.IsValid)
            {
                movies.title=title,
                movies.Genre=Genre,
                movies.ReleaseDate=ReleaseDate,
                movies.price=price,
                movies.Ratings=Ratings
                db.Movies.Add(movies);
                db.SaveChanges();
                return RedirectToAction(&amp;amp;quot;Index&amp;amp;quot;);
            }

            return View(movies);
        }

Please feel free to comment	;-)
 
Share this answer
 
Comments
Anosike Osifo 25-Feb-13 15:49pm    
For the solution proffered, please i think it will be very tedious when the form contains a large nos of properties to be saved. Please any detailed info/ alternate method to achieve the saving records without having to assign them explicitly. thanks. I'm facing the same challenge as the original author of this Question

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900