当我的视图将模型发布回我的操作时,如何将其保存回其来源的数据库中?

发布于 2024-08-21 00:25:50 字数 1568 浏览 10 评论 0原文

我有点困惑...

我有一个操作,它需要一个 ID,加载一个对象,并将其传递给绑定到该对象类型的模型的视图。

在编辑视图提供的表单中的数据后,我将 POST 回另一个接受与模型完全相同类型的对象的操作。

然而,此时我不能只调用 Repository.Save,我想我现在有了一个全新的对象,不再与发送到视图的原始数据库查询中的对象相关联。

那么如何更新之前查询的对象并将更改保存到数据库而不是从视图中获取新对象呢?

我什至尝试从数据库获取对象的新实例并将 View 返回的对象分配给它,然后 Repo.Save() ,仍然没有这样的运气。

我在这里做错了什么?

控制器代码:

[Authorize]
public ActionResult EditCompany(int id)
{
    //If user is not in Sys Admins table, don't let them proceed
    if (!userRepository.IsUserSystemAdmin(user.UserID))
    {
        return View("NotAuthorized");
    }

    Company editThisCompany = companyRepository.getCompanyByID(id);

    if (editThisCompany == null)
    {
        RedirectToAction("Companies", new { id = 1 });
    }

    if (TempData["Notify"] != null)
    {
        ViewData["Notify"] = TempData["Notify"];
    }

    return View(editThisCompany);
}

//
// POST: /System/EditCompany

[Authorize]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult EditCompany(Company company)
{
    string errorResponse = "";

    if (!isCompanyValid(company, ref errorResponse))
    {
        TempData["Notify"] = errorResponse;
        return RedirectToAction("EditCompany", new { id = company.CompanyID });
    }
    else
    {
        Company updateCompany = companyRepository.getCompanyByID(company.CompanyID);
        updateCompany = company;
        companyRepository.Save();
        return RedirectToAction("EditCompany", new { id = company.CompanyID });
    }


    return RedirectToAction("Companies", new { id = 1 });
}

I'm kind of confused...

I have one action that takes an ID, loads up an object, and passes it to the View which is bound to the Model of that object's type.

After editing the data in the form supplied by the View, I POST back to another action that accepts an object of the same exact type as the Model.

However at this point I can't just call Repository.Save, I think I have a brand new object now, no longer associated with the one from the original database query that was sent to the View.

So how can I update the previously queried object and save the changes to the DB instead of getting a new object back from the View?

I even tried getting a new instance of the object from the DB and assigning the View returned object to it, and then Repo.Save(), still no such luck.

What am I doing wrong here?

CONTROLLER CODE:

[Authorize]
public ActionResult EditCompany(int id)
{
    //If user is not in Sys Admins table, don't let them proceed
    if (!userRepository.IsUserSystemAdmin(user.UserID))
    {
        return View("NotAuthorized");
    }

    Company editThisCompany = companyRepository.getCompanyByID(id);

    if (editThisCompany == null)
    {
        RedirectToAction("Companies", new { id = 1 });
    }

    if (TempData["Notify"] != null)
    {
        ViewData["Notify"] = TempData["Notify"];
    }

    return View(editThisCompany);
}

//
// POST: /System/EditCompany

[Authorize]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult EditCompany(Company company)
{
    string errorResponse = "";

    if (!isCompanyValid(company, ref errorResponse))
    {
        TempData["Notify"] = errorResponse;
        return RedirectToAction("EditCompany", new { id = company.CompanyID });
    }
    else
    {
        Company updateCompany = companyRepository.getCompanyByID(company.CompanyID);
        updateCompany = company;
        companyRepository.Save();
        return RedirectToAction("EditCompany", new { id = company.CompanyID });
    }


    return RedirectToAction("Companies", new { id = 1 });
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情绪 2024-08-28 00:25:50

尝试使用 TryUpdateModel 方法。通过这种方式,您可以在进行数据绑定之前从存储库中获取该公司。

[Authorize]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult EditCompany(int id, FormCollection form)
{
    //Default to a new company
    var company = new Company();

    //If we have an id, we must be editing a company so get it from the repo
    if (id > 0)
        company = companyRepository.getCompanyByID(id);

    //Update the company with the values from post
    if (TryUpdateModel(company, form.ToValueProvider()))
    {
        string errorResponse = "";

        if (!isCompanyValid(company, ref errorResponse))
        {
            TempData["Notify"] = errorResponse;
            return RedirectToAction("EditCompany", new { id = company.CompanyID });
        }
        else
        {
            companyRepository.Save();
            return RedirectToAction("EditCompany", new { id = company.CompanyID });
        }
    }

    return RedirectToAction("Companies", new { id = 1 });
}

HTH,
查尔斯

·诗篇。通常,将数据绑定到域模型是一个坏主意......改为使用表示模型,然后您就可以解决整个问题。

Try using the TryUpdateModel method. This way you can get the company from the repository before you databind to it.

[Authorize]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult EditCompany(int id, FormCollection form)
{
    //Default to a new company
    var company = new Company();

    //If we have an id, we must be editing a company so get it from the repo
    if (id > 0)
        company = companyRepository.getCompanyByID(id);

    //Update the company with the values from post
    if (TryUpdateModel(company, form.ToValueProvider()))
    {
        string errorResponse = "";

        if (!isCompanyValid(company, ref errorResponse))
        {
            TempData["Notify"] = errorResponse;
            return RedirectToAction("EditCompany", new { id = company.CompanyID });
        }
        else
        {
            companyRepository.Save();
            return RedirectToAction("EditCompany", new { id = company.CompanyID });
        }
    }

    return RedirectToAction("Companies", new { id = 1 });
}

HTHs,
Charles

Ps. generally it's a bad idea to databind to your domain models... use presentation models instead and then you can get around this whole issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文