[英]how to save data from two models (relation one to many) from one viewModel in MVC c#?
我只需要一个视图,然后:
但是我不知道我的保存操作出了什么问题。 如何将客户的AddressId设置为新的地址ID(已创建)?
我用:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public DbSet<Customer> Customers { get; set; }
public DbSet<Address> Addresses { get; set; }
}
我有两个模型:
public class Customer
{
public int Id { get; set; }
[Required]
[StringLength(255)]
public string Name { get; set; }
public Address Address { get; set; }
public int? AddressId { get; set; }
}
public class Address
{
public int Id { get; set; }
[Required]
[StringLength(255)]
public string Town { get; set; }
}
还有一个viewModel
public class CustomerAddressViewModels
{
public Customer Customer { get; set; }
public Address Addresses { get; set; }
}
然后,我创建一个具有良好“详细信息”操作的控制器
public class CustomerDetailsController : Controller
{
private ApplicationDbContext db = new ApplicationDbContext();
// GET: CustomerDetails/Details/5
public async Task<ActionResult> Details(int id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Customer customer = await db.Customers
.Include(c => c.Address)
.SingleOrDefaultAsync(c => c.Id == id);
return View("CustomerAddressView");
}
}
我为创建和更新案例编写了保存操作:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Save(Customer customer, Address address)
{
if (!ModelState.IsValid)
{
var vieModel = new CustomerAddressViewModels();
return View("CustomerAddressView", vieModel);
}
if (customer.AddressId == 0)
{
address.StreetName = customer.Address.StreetName;
db.Addresses.Add(address);
}
else
{
var addressInDb = db.Addresses
.Single(a => a.Id == customer.AddressId);
addressInDb.StreetName = customer.Address.StreetName;
}
if (customer.Id == 0)
{
db.Customers.Add(customer);
}
else
{
var customerInDb = db.Customers
.Single(c => c.Id == customer.Id);
customerInDb.Name = customer.Name;
customerInDb.AddressId = customer.AddressId;
}
await db.SaveChangesAsync();
return RedirectToAction("Index","Customers");
}
我只需要一个视图,然后:
但是我不知道我的保存操作出了什么问题。 如何将客户的AddressId设置为新的地址ID(已创建)?
插入地址时,应将地址添加到customer.Addresses
集合中,而不是直接添加到db.Address
,然后EF应该为您处理填充键。
我认为这里还有其他值得一提的事情。
您应该使用viewmodel类来表示要传递给视图和从视图传递的对象,而不是直接使用实体。 因此,我建议像这样的课程:
public class CustomerViewModel { public int CustomerOd { get; set; } //<... other properties for customer> public AddressViewModel Address { get; set; } } public class AddressViewModel { //Address propties }
这使您可以查看对象上的特定属性,这些属性可以基于不在客户实体内部的值来帮助处理各种事情(例如,是隐藏还是显示节)。
然后,您的控制器动作保存如下所示:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Save(CustomerViewModel model)
{
//preceeding code
var customer = db.Customers.SingleOrDefault(x => x.CustomerId == model.CustomerId)
if (customer.Address == null)
{
var address = new Address()
{
StreetName = model.Address.StreetName
};
Customer.Addresses.Add(address);
}
}
然后,您可以将数据从ViewModel对象映射或填充到您的Entity。 它涉及一些额外的工作,但具有更大的灵活性。
您的“详细信息”操作中发生了什么? 您获得了客户,但是却没有将其传递给您?
Customer customer = await db.Customers
.Include(c => c.Address)
.SingleOrDefaultAsync(c => c.Id == id);
return View("CustomerAddressView");
为了使框架正确地将您的对象从表单绑定到地址对象的视图,应使用与对象相同的结构描述html对象。 所以你可以有这样的输入
<input value="@Model.Address.StreetName" name="Address.StreetName"/>
当您发回邮件时,它应该将该值绑定到地址对象。
希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.