简体   繁体   English

ASP.net MVC3如何编辑SQL数据库

[英]ASP.net MVC3 howto edit sql database

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. 这是我编写的用于编辑电影详细信息和更新sql server中的数据库的代码。 This dont have any compile errors, But It didnt update sql server database. 这没有任何编译错误,但是它没有更新sql server数据库。

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 ie MovieDb.Categories.SaveChanges() and pass through the item that you want to update. 然后,您可以在该实体(即MovieDb.Categories.SaveChanges())上调用savechanges方法,并传递要更新的项目。

You need to use the Model2 object to create a new entity, add it to the ObjectContext and save the changes. 您需要使用Model2对象创建一个新实体,将其添加到ObjectContext并保存更改。 You haven't written any code that should save anything to a database. 您尚未编写任何应将任何内容保存到数据库的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM