我是MVC和EF世界的新手。我首先使用代碼針對MVC 4 EF 5。在一個視圖中編輯/列出多個模型
我正在尋找使用一個視圖編輯兩個相關模型的最佳做法。爲了簡單起見,我有以下兩種模式:
namespace AddressBook.Models
{
public class Contact
{
public int ID { get; set; }
public string First_Name { get; set; }
public string Last_Name { get; set; }
public List<PhoneNumber> PhoneNumbers { get; set; }
}
}
和
namespace AddressBook.Models
{
public class PhoneNumber
{
public int ID { get; set; }
public string Number { get; set; }
public bool Primary { get; set; }
}
}
有以下方面:
using System.Data.Entity;
namespace AddressBook.Models
{
public class DataContext : DbContext
{
public DbSet<Contact> Contacts { get; set; }
public DbSet<PhoneNumber> PhoneNumbers { get; set; }
}
}
的聯繫和PhoneNumber之間的關係是一對多的,不過我想要在Primary設置爲true時能夠編輯first_name,last_name和Number,所以我們將僅編輯每個聯繫人記錄的一個電話號碼。
我看過類似的帖子指向使用ViewModel,但我所看到的viewmodels的唯一例子是在傳遞下拉信息時使用而不是viewbag。
我想我有幾個問題:
將視圖模型看起來像下面?
public class ContactPrimaryNumberViewModel { public Contact ContactToEdit {get; set;} public PhoneNumber PhoneNumberToEdit {get;set;} }
什麼是編輯(後)和編輯(get)的樣子?
任何幫助,將不勝感激幫助我滿腦子都在這...
這裏編輯(GET)修改爲支持,如果聯繫人沒有電話號碼相關聯
'// GET:/聯繫人/編輯/ 5
public ActionResult Edit(int id = 0)
{
ContactPrimaryNumberViewModel ContactPrimaryNumber = (from pn in db.PhoneNumbers
where pn.ContactID == id && pn.Primary == true
select new ContactPrimaryNumberViewModel { ContactID = pn.ContactID, First_Name = pn.Contact.First_Name, Last_Name = pn.Contact.Last_Name, Number = pn.Number }).SingleOrDefault();
if (ContactPrimaryNumber == null)
{
ContactPrimaryNumber = (from c in db.Contacts
where c.ID == id
select new ContactPrimaryNumberViewModel { ContactID = c.ID, First_Name = c.First_Name, Last_Name = c.Last_Name, Number = null }).Single();
}
return View(ContactPrimaryNumber);
}'
所以最終大家的解決方案後,幫助:
型號:
public class PhoneNumber
{
public int ID { get; set; }
public string Number { get; set; }
public bool Primary { get; set; }
[Required]
public int ContactID { get; set; }
public Contact Contact { get; set; }
}
public class Contact
{
public int ID { get; set; }
public string First_Name { get; set; }
public string Last_Name { get; set; }
public List<PhoneNumber> PhoneNumbers { get; set; }
}
但是控制器編輯(get和post )
// GET: /Contact/Edit/5
public ActionResult Edit(int id = 0)
{
ContactPrimaryNumberViewModel ContactPrimaryNumber = (from c in db.Contacts
join pn in db.PhoneNumbers
on c.ID equals pn.ContactID into outer
from _pn in outer.Where(p => p.Primary ==true).DefaultIfEmpty()
where c.ID == id
select new ContactPrimaryNumberViewModel { ContactID = c.ID, First_Name = c.First_Name, Last_Name = c.Last_Name, Number = ((_pn == null) ? "" : _pn.Number) }).FirstOrDefault();
if (ContactPrimaryNumber == null)
{
return HttpNotFound();
}
return View(ContactPrimaryNumber);
}
// POST: /Contact/Edit/5
[HttpPost]
public ActionResult Edit(ContactPrimaryNumberViewModel ContactPrimaryNumber)
{
Contact c = db.Contacts.Find(ContactPrimaryNumber.ContactID);
PhoneNumber pn = db.PhoneNumbers.FirstOrDefault(x => x.ContactID == ContactPrimaryNumber.ContactID && x.Primary == true);
if (ModelState.IsValid)
{
c.First_Name = ContactPrimaryNumber.First_Name;
c.Last_Name = ContactPrimaryNumber.Last_Name;
if (pn == null) //if there is no phone number associated with the contact in the DB
{
if (!String.IsNullOrEmpty(ContactPrimaryNumber.Number))
{
//Add a new phonenumber in the database
PhoneNumber Px = new PhoneNumber();
Px.ContactID = ContactPrimaryNumber.ContactID;
Px.Number = ContactPrimaryNumber.Number;
Px.Primary = true;
db.PhoneNumbers.Add(Px);
}
}
else //if there is a phone number associated with the contactin the DB
{
if (String.IsNullOrEmpty(ContactPrimaryNumber.Number))
{
//delete the existing number
db.PhoneNumbers.Remove(pn);
}
else
{
//modify the existing number
pn.Number = ContactPrimaryNumber.Number;
}
}
db.SaveChanges();
return RedirectToAction("Index");
}
return View(c);
}
和視圖模型
public class ContactPrimaryNumberViewModel
{
public int ContactID { get; set; }
public string First_Name { get; set; }
public string Last_Name { get; set; }
public string Number { get; set; }
}
再次
感謝您的幫助
首先,您需要PhoneNumber中的聯繫人外鍵,然後才能擁有自定義保存方法。我會寫在幾個 – Komengem
嗨Komenge ...感謝您的答覆...我只注意到外鍵也當我試圖生成scafolding和它沒有創造viewbag我automaticaly看起來像現在這樣「公共類******中國 { 公衆詮釋ID {獲得;組; } public string Number {get;組; } public bool Primary {get;組; } [必須] public int ContactID {get;組; } public Contact {get;組; } }」 – user2129585
MHMM一個問題之前,我給你寫我的回答,爲什麼接觸需要有多個號碼?它真的有必要嗎? – Komengem