通过表单更新视图?
Updatable View via Form?
是否可以通过表单更新数据而不将表单绑定到原始表单table?
我们需要用户更新 SQL 视图映射到的主要 table 中的一小部分数据,下面是用于更新数据的代码示例:
[HttpPost]
public ActionResult Address(AddressView model)
{
if (ModelState.IsValid)
{
TestEntities.AddressView .AddObject(model);
TestEntities.SaveChanges();
return Redirect("/Customer?id=" + model.id);
}
return View(model);
}
不幸的是,由于视图不能包含主键,我在尝试更新数据时遇到以下错误:
Unable to update the EntitySet 'AddressView' because it has a DefiningQuery and no element exists in the element to support the current operation.
有没有不使用典型 SQL 更新语句的解决方法,如果可能的话我想坚持使用 Entity Framework。
这是我的解决方法,主要是使用初始视图中的数据将数据呈现给模型,然后在 post 上将数据映射到实际 table(在初始视图中使用)包含一个主键:
public ActionResult Customer(int id)
{
var viewModel = new Customer();
using (var testEntities = new TestEntities(new EntityConnection(StrEntities)))
{
var model = testEntities.CustomerView.Single(m => m.id == id);
viewModel.id = id;
viewModel.address_1 = model.address_1;
viewModel.address_2 = model.address_2;
viewModel.post_code = model.post_code;
}
return View("Customer", viewModel);
}
[HttpPost]
public ActionResult Address(Address viewModel)
{
if (ModelState.IsValid)
{
using (var testEntities = new TestEntities(new EntityConnection(StrEntities)))
{
var model = testEntities.AddressTable.Single(m => m.id == viewModel.id);
model.address_1 = viewModel.address_1;
model.address_2 = viewModel.address_2;
model.post_code = viewModel.post_code;
testEntities.SaveChanges();
}
return Redirect("/Customer?id=" + viewModel.id);
}
return View(viewModel);
}
希望这对其他人有用:-)
是否可以通过表单更新数据而不将表单绑定到原始表单table?
我们需要用户更新 SQL 视图映射到的主要 table 中的一小部分数据,下面是用于更新数据的代码示例:
[HttpPost]
public ActionResult Address(AddressView model)
{
if (ModelState.IsValid)
{
TestEntities.AddressView .AddObject(model);
TestEntities.SaveChanges();
return Redirect("/Customer?id=" + model.id);
}
return View(model);
}
不幸的是,由于视图不能包含主键,我在尝试更新数据时遇到以下错误:
Unable to update the EntitySet 'AddressView' because it has a DefiningQuery and no element exists in the element to support the current operation.
有没有不使用典型 SQL 更新语句的解决方法,如果可能的话我想坚持使用 Entity Framework。
这是我的解决方法,主要是使用初始视图中的数据将数据呈现给模型,然后在 post 上将数据映射到实际 table(在初始视图中使用)包含一个主键:
public ActionResult Customer(int id)
{
var viewModel = new Customer();
using (var testEntities = new TestEntities(new EntityConnection(StrEntities)))
{
var model = testEntities.CustomerView.Single(m => m.id == id);
viewModel.id = id;
viewModel.address_1 = model.address_1;
viewModel.address_2 = model.address_2;
viewModel.post_code = model.post_code;
}
return View("Customer", viewModel);
}
[HttpPost]
public ActionResult Address(Address viewModel)
{
if (ModelState.IsValid)
{
using (var testEntities = new TestEntities(new EntityConnection(StrEntities)))
{
var model = testEntities.AddressTable.Single(m => m.id == viewModel.id);
model.address_1 = viewModel.address_1;
model.address_2 = viewModel.address_2;
model.post_code = viewModel.post_code;
testEntities.SaveChanges();
}
return Redirect("/Customer?id=" + viewModel.id);
}
return View(viewModel);
}
希望这对其他人有用:-)