Asp.net mvc MVC Homecontroller在数据库中编辑详细信息

Asp.net mvc MVC Homecontroller在数据库中编辑详细信息,asp.net-mvc,ckeditor,Asp.net Mvc,Ckeditor,所以,我们只是使用了一个基本的教程来创建一个数据库并向其中添加新的记录。这一切都很好…但现在我想编辑一个记录 // GET: /Home/Edit/5 public ActionResult Edit(int id) { return View(); } // // POST: /Home/Edit/5 [HttpPost] //public ActionResult Edit(int id, FormCollecti

所以,我们只是使用了一个基本的教程来创建一个数据库并向其中添加新的记录。这一切都很好…但现在我想编辑一个记录

// GET: /Home/Edit/5

    public ActionResult Edit(int id)
    {
        return View();
    }

    //
    // POST: /Home/Edit/5

    [HttpPost]
    //public ActionResult Edit(int id, FormCollection collection)
    public ActionResult Edit([Bind(Exclude = "id")]MovieTable1 movieToCreate)
    {
        try
        {
            // TODO: Add update logic here

           //so i need to insert a line of code here to say something like update
            //I was using the previous code:
           //_entities.AddToMovieTable1(movieToCreate);
            _entities.SaveChanges();           

            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }
这不起作用,我想我所要做的就是编辑行“_entities.AddToMovieTable1(movieToCreate);”要编辑记录,请执行以下操作:

为了使添加新记录生效,我使用了以下代码

 public class HomeController : Controller
{
    private MoviesDBEntities _entities = new MoviesDBEntities();
    //
    // GET: /Home/

    public ActionResult Index()
    {
        return View(_entities.MovieTable1.ToList());
    }

    //
    // GET: /Home/Details/5

    public ActionResult Details(int id)
    {
        return View();
    }

    //
    // GET: /Home/Create

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

    //
    // POST: /Home/Create

    [HttpPost]
    //public ActionResult Create(FormCollection collection)
    public ActionResult Create([Bind(Exclude = "id")]MovieTable1 movieToCreate)
    {
        try
        {
            // TODO: Add insert logic here
            _entities.AddToMovieTable1(movieToCreate);
            _entities.SaveChanges();

            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }
您可以使用:

_entities.Attach(moveiToModify);
_entities.Entry(moveiToModify).State = EntityState.Modified;
我真的不知道,为什么你要排除ID属性,它是更新所必需的