[英]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.