0
總是在同一個項目上(對於那些回答我的人),我有一個與實體VehicleFuelCard(一對多關係)有關的實體「Person」。在我的「創建人」的說法,我包括通過AJAX代碼這實際上是一個「創建加油卡」視圖的局部視圖:從局部視圖添加數據
<script type = "text/javascript">
function AddVehicleFuelCard() {
$("#test").load("@Url.Action("AddVehicleFuelCard","Person",
new { personViewModel = Model })");
}
</script>
,並調用它在PersonController:
public ActionResult AddVehicleFuelCard(PersonViewModel personViewModel)
{
ViewBag.Id_VehicleFuelCardCompany = new SelectList(db.VehicleFuelCardCompanies, "Id_VehicleFuelCardCompany", "Name");
return PartialView("PartialVehicleFuelCard", personViewModel);
}
這裏是視圖模型,我使用:
public class PersonViewModel
{
private Person _person;
public List<VehicleFuelCard> listVehicleFuelCard;
public Person Person
{
get
{
return this._person;
}
}
public PersonViewModel()
{
this._person = new Person();
}
public PersonViewModel(Person person)
{
this._person = person;
}
public VehicleFuelCard NewVehicleFuelCard
{
get
{
VehicleFuelCard element = new VehicleFuelCard();
element.Person = this.Person;
Person.VehicleFuelCards.Add(element);
return element;
}
}
public PhoneCard NewPhoneCard
{
get
{
PhoneCard element = new PhoneCard();
Person.PhoneCards.Add(element);
return element;
}
}
public ProductAllocation NewProductAllocation
{
get
{
ProductAllocation element = new ProductAllocation();
Person.ProductAllocations.Add(element);
return element;
}
}
}
而建立在我的控制器操作:
public ActionResult Create()
{
ViewBag.Id_ProductPackageCategory = new SelectList(db.ProductPackageCategories, "Id_ProductPackageCategory", "Name");
return View();
}
//
// POST: /Person/Create
[HttpPost]
public ActionResult Create(PersonViewModel personViewModel)
{
if (ModelState.IsValid)
{
db.Persons.AddObject(personViewModel.Person);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.Id_ProductPackageCategory = new SelectList(db.ProductPackageCategories, "Id_ProductPackageCategory", "Name", personViewModel.Person.Id_ProductPackageCategory);
return View(personViewModel);
}
在添加人員時添加加油卡的任何想法?謝謝 !
謝謝...我想我必須做一些研究,因爲我沒有抓住你所說的一切。但是,謝謝。 – Traffy 2013-03-04 15:26:30
np,我已經擴展了我的答案。 – Ulises 2013-03-04 15:44:36
謝謝,我會試試這個! – Traffy 2013-03-04 15:52:21