使用DbContext和POCO类创建新记录时更新模型值

时间:2012-03-15 17:40:05

标签: c# asp.net-mvc entity-framework poco

1)

public ActionResult Create(Job job)
{
    if (ModelState.IsValid)
    {
        var innerJob = new Job();
        innerJob.JobID = 100;
        TryUpdateModel(innerJob);


        db.Jobs.Add(innerJob);
        db.SaveChanges();
        return RedirectToAction("Index");  
    }

    ViewBag.ClientID = new SelectList(db.Client, "ClientID", "ClientName", job.ClientID);
    ViewBag.JobOwner = new SelectList(db.User, "UserID", "LoginName", job.JobOwner);
    return View(job);
}

2)

public ActionResult Create(Job job)
{
    if (ModelState.IsValid)
    {
        job.JobID = 100;
        db.Jobs.Add(job);
        db.SaveChanges();
        return RedirectToAction("Index");  
    }

    ViewBag.ClientID = new SelectList(db.Client, "ClientID", "ClientName", job.ClientID);
    ViewBag.JobOwner = new SelectList(db.User, "UserID", "LoginName", job.JobOwner);
    return View(job);
}

在上面两个视图中,“作业”是我的模型来自视图。现在我想在新创建的记录中插入“Id”。我怎么能这样做?

以上两种方法对我都不起作用。我正在使用EF 4.3和DataContext。

0 个答案:

没有答案