当我的视图将模型发布回我的操作时,如何将其保存回其来源的数据库中?
我有点困惑...
我有一个操作,它需要一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用
TryUpdateModel
方法。通过这种方式,您可以在进行数据绑定之前从存储库中获取该公司。HTH,
查尔斯
·诗篇。通常,将数据绑定到域模型是一个坏主意......改为使用表示模型,然后您就可以解决整个问题。
Try using the
TryUpdateModel
method. This way you can get the company from the repository before you databind to it.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.