2
MovieStoreEntities MovieDb = new MovieStoreEntities();
public ActionResult Edit(int id)
{

    //var EditMovie1 = MovieDb
    AddMovieModel EditMovie = (from M in MovieDb.Movies
                               from C in MovieDb.Categories
                               where M.CategoryId == C.Id
                               where M.Id == id
                               select new AddMovieModel { Name = M.Name, Director = M.Director, Country = M.Country, categorie = C.CategoryName, Category = M.CategoryId }).FirstOrDefault();

    //AddMovieModel EditMovie1 = MovieDb.Movies.Where(m => m.Id == id).Select(m => new AddMovieModel {m.Id   }).First();
    List<CategoryModel> categories = MovieDb.Categories
       .Select(category => new CategoryModel { Category = category.CategoryName, id = category.Id })
       .ToList();

    ViewBag.Category = new SelectList(categories, "Id", "Category");


    return View(EditMovie);
}

//
// POST: /Default1/Edit/5

[HttpPost]
public ActionResult Edit(AddMovieModel Model2)
{
    List<CategoryModel> categories = MovieDb.Categories
        .Select(category => new CategoryModel { Category = category.CategoryName, id = category.Id })
        .ToList();
    ViewBag.Category = new SelectList(categories, "Id", "Category");



    if (ModelState.IsValid)
    {
        //MovieStoreEntities model = new MovieStoreEntities();






        MovieDb.SaveChanges();


        return View("Thanks2", Model2);
    }
    else
        return View();

}

This is the Code that I have wrote to edit Movie details and update database in the sql server. This dont have any compile errors, But It didnt update sql server database.

2 Answers 2

4

Presuming here you are updating a category you would need to do something like

 List<CategoryModel> categories = MovieDb.Categories
    .Select(category => new CategoryModel { Category = category.CategoryName, id = category.Id })
    .ToList();
ViewBag.Category = new SelectList(categories, "Id", "Category")
Category category = new Category()
category = categories.First(p=>p.CategoryId == Id);
category.Name = "New Name";
MovieDb.Categories.SaveChanges(category);
MovieDb.SaveChanges();

You will need to get the item you are wanting to edit...in this case a category which would be filtered from the list of categories. You can then call the savechanges method on that entity i.e. MovieDb.Categories.SaveChanges() and pass through the item that you want to update.

Sign up to request clarification or add additional context in comments.

Comments

0

You need to use the Model2 object to create a new entity, add it to the ObjectContext and save the changes. You haven't written any code that should save anything to a database.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.