2

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

// 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();
        }
    }
4

1 回答 1

2

你可以使用:

_entities.Attach(moveiToModify);
_entities.Entry(moveiToModify).State = EntityState.Modified;

我真的不知道,为什么要排除 ID 属性,它是更新所必需的。

于 2012-10-20T01:06:57.220 回答