Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET SQL-Server-2008 MVC , +
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
 
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
 
 //
        // 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
@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 15-Jan-13 6:46am
ExpertITM1.2K
Edited 15-Jan-13 6:58am
v2
Comments
Zoltán Zörgő at 15-Jan-13 14:06pm
   
What do you mean by "where to store data after called savechanges"?
JemiM at 17-Jan-13 1:26am
   
Please improve your question. What happened while saving?Any Exception?

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You are trying to save it without passing any value.
just modify you post method as
 
[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	;-)
  Permalink  
Comments
Anosike Osifo at 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)

  Print Answers RSS
0 OriginalGriff 200
1 Kamal Rocks 184
2 PIEBALDconsult 150
3 BillWoodruff 148
4 Jochen Arndt 135
0 OriginalGriff 5,695
1 DamithSL 4,506
2 Maciej Los 4,007
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 16 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100