English 中文(简体)
如何根据传递给Edit操作方法的域模型参数更新域模型对象?
原标题:How to update the domain model object based on the domain model parameter passed to Edit action method?

我有一个操作方法来处理HTTP-POST,如下所示。

    [HttpPost]
    public ActionResult Edit(Movie model)
    {
        var movie = db.Movies.FirstOrDefault(x => x.Id == model.Id);

        if (movie == null)
        {
            TempData["MESSAGE"] = "No movie with id = " + id + ".";
            return RedirectToAction("Index", "Home");
        }

        if (!ModelState.IsValid)
            return View(model);

        // what method do I have to invoke here
        // to update the movie object based on the model parameter?
        db.SaveChanges();
        return RedirectToAction("Index");
    }

问题:如何在模型的基础上更新电影

编辑1

基于@lukled的解决方案,以下是最终的工作代码:

    [HttpPost]
    public ActionResult Edit(Movie model)
    {
        var movie = db.Movies.FirstOrDefault(x => x.Id == model.Id);

        if (movie == null)
        {
            TempData["MESSAGE"] = string.Format("There is no Movie with id = {0}.", movie.Id);
            return RedirectToAction("Index", "Home");
        }

        if (!ModelState.IsValid)
            return View(model);

        var entry = db.Entry(movie);
        entry.CurrentValues.SetValues(model);
        db.SaveChanges();
        return RedirectToAction("Index");
    }
最佳回答

试试这个:

db.Movies.ApplyCurrentValues(model);
db.SaveChanges();

您也可以将值从模型复制到电影:

movie.Title = model.Title;
movie.Director = model.Director;
db.SaveChanges();

好的。您使用的是“代码优先”,因此可能是:

var entry = context.Entry(movie);
entry.CurrentValues.SetValues(model);
db.SaveChanges();

但我不确定,因为我没有安装代码优先。取自:

http://blogs.msdn.com/b/adonet/archive/2011/01/30/using-dbcontext-in-ef-feature-ctp5-part-5-working-with-property-values.aspx

问题回答
[HttpPost]
public ActionResult Edit(Movie movie)
{

    if (movie == null)
    {
        TempData["MESSAGE"] = "No movie with id = " + id + ".";
        return RedirectToAction("Index", "Home");
    }

    if (!ModelState.IsValid)
        return View(movie);

    // what method do I have to invoke here
    // to update the movie object based on the model parameter?

    db.Movie.AddObject(movie);
    db.ObjectStateManager.ChangeObjectState(movie, System.Data.EntityState.Modified);

    db.SaveChanges();
    return RedirectToAction("Index");
}

你试过了吗

TryUpdateModel(movie)




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签