Asp.net 从viewmodel更新数据库,但也添加新记录

Asp.net 从viewmodel更新数据库,但也添加新记录,asp.net,asp.net-mvc,asp.net-mvc-3,Asp.net,Asp.net Mvc,Asp.net Mvc 3,我有一个视图,其中有一个项目列表(可以通过jQuery动态添加到) 当我将viewmodel发布回控制器时,如果代码找不到ID,如何插入新项并将它们保存到数据库中 我的初始代码如下-更新已保存,但新项目未保存: // // POST: /Objective/Edit/model [HttpPost] public ActionResult Edit(ObjectivesEdit model) { if (model.Objectives !

我有一个视图,其中有一个项目列表(可以通过jQuery动态添加到)

当我将viewmodel发布回控制器时,如果代码找不到ID,如何插入新项并将它们保存到数据库中

我的初始代码如下-更新已保存,但新项目未保存:

    //
    // POST: /Objective/Edit/model
    [HttpPost]
    public ActionResult Edit(ObjectivesEdit model)
    {
        if (model.Objectives != null)
        {
             foreach (var item in model.Objectives)
            {
                // find the database row
                Objective objective = db.objectives.Find(item.ID);
                if (objective != null) 
                {
                    // Set the database row to the posted values

                    objective.objective = item.objective;
                    objective.score = item.score;
                    objective.possscore = item.possscore;
                }
                else  // database item not found, so add a new item
                {
                    // add a new objective 
                    // This doesn't seem to add/save a new record
                    Objective obj = new Objective();
                    obj.objective = item.objective;
                    obj.score = item.score;
                    obj.possscore = item.possscore;
                }
             }
            // Save the changes to the database
            db.SaveChanges();
        }
        return View(model);
    }
谢谢你的帮助


标记

您不会将新创建的目标添加到您的上下文中

else  // database item not found, so add a new item
{
    // add a new objective 
    // This doesn't seem to add/save a new record
    Objective obj = new Objective();
    obj.objective = item.objective;
    obj.score = item.score;
    obj.possscore = item.possscore;

    // Missing line.
    db.objectives.Add(obj);
 }  
如果您正在使用EF 4.0(即
db
属于
ObjectContext
类型),则应使用
db.AddObject(obj)

根据您的评论进行更新:
一种方法是在保存更改后检索所有添加的项。另一种方法是在创建新目标时修改模型。更改的零件标有*:

foreach (var item in model.Objectives.ToList()) // *:Notice the ToList()
{
    // find the database row
    Objective objective = db.objectives.Find(item.ID);
    if (objective != null) 
    {
        // Set the database row to the posted values

        objective.objective = item.objective;
        objective.score = item.score;
        objective.possscore = item.possscore;
    }
    else  // database item not found, so add a new item
    {
          // add a new objective 
          // This doesn't seem to add/save a new record
          Objective obj = new Objective();
          obj.objective = item.objective;
          obj.score = item.score;
          obj.possscore = item.possscore;

         db.AddObject(obj)
         // Save the changes to the database
        db.SaveChanges(); // *: save in loop to get thee ID.

        item.ID = obj.ID; // *: assign the ID to the model.
     }
}

 return View(model);

干杯-我知道一定是这么简单。。。谢谢你,MarkHi-一个后续查询-当我返回视图(模型)时;我是否可以让它返回模型,包括主键的新数据库ID?因为它刚才似乎只返回0-这显然不是保存到数据库中的内容?谢谢你,马克