Tek bir görünüm modeli oluşturmak için bir araya gelen üç modelim var ve "düzenle" yi tıklattığınızda bu görünüm modelini düzenleyebilmek istiyorum. Bunun nasıl çalıştığını (herhangi bir yerde) doğru bir örnek bulamıyorum.MVC3'teki düzenleme sayfasındaki görünüm modelini nasıl güncellerim?
Doğru yoldan aşağı gidiyorum emin değilim. Görünümü veri ile elde edebiliyorum. Bu noktada, onu kaydedemiyorum.
Herhangi bir yardım için teşekkür ederiz.
Teşekkürler!
Modeller:
public class Person
{
[Key]
public int Id { get; set; }
[MaxLength(20)]
[Required(ErrorMessage = "First name is required.")]
public string FirstName { get; set; }
[MaxLength(20)]
[Required(ErrorMessage = "Last name is required.")]
public string LastName { get; set; }
[MaxLength(40)]
[Required(ErrorMessage = "Email is required.")]
public string Email { get; set; }
[MaxLength(20)]
[DataType(DataType.PhoneNumber)]
public string Phone { get; set; }
public bool Active { get; set; }
}
public class ClientContact
{
[Key]
[ForeignKey("Person")]
public int ClientPersonId { get; set; }
public int ClientId { get; set; }
[MaxLength(40)]
public string Title { get; set; }
public Person Person { get; set; }
[ForeignKey("ClientId")]
public Client Client { get; set; }
}
public class Client
{
[Key]
public int ClientId { get; set; }
public string Name { get; set; }
public bool Active {get;set;}
}
Görünüm Modeli:
public class ClientContactViewModel
{
private SimplexDB db = new SimplexDB();
public ClientContactViewModel()
{
}
public ClientContactViewModel(int id)
{
ClientPersonId = id;
InitializeClientContact();
}
public int ClientPersonId { get; set; }
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Display(Name = " Last Name")]
public string LastName { get; set; }
[Display(Name = "Title")]
public string Title { get; set; }
[Display(Name = "Email Address")]
public string Email { get; set; }
[Display(Name = "Phone")]
public string Phone { get; set; }
[Display(Name = "Client Name")]
public int ClientId { get; set; }
public SelectList Clients
{
get
{
return new SelectList(db.Clients, "ClientId", "Name");
}
}
private void InitializeClientContact()
{
var contact = db.ClientPersons.Include("Person").Where(x => x.ClientPersonId == ClientPersonId).SingleOrDefault();
if (contact != null)
{
FirstName = contact.Person.FirstName;
LastName = contact.Person.LastName;
Title = contact.Title;
Email = contact.Person.Email;
Phone = contact.Person.Phone;
ClientId = contact.ClientId;
}
}
}
Denetleyici:
public class ClientContactController : Controller
{
private database db = new database();
//
// GET: /ClientContact/Edit/5
public ActionResult Edit(int id)
{
return View(new ClientContactViewModel(id));
}
//
// POST: /ClientContact/Edit/5
[HttpPost]
public ActionResult Edit(ClientContactViewModel model)
{
if (ModelState.IsValid)
{
db.Entry(model).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(model);
}
}
Ben db.Entry (modele) bir hata alıyorum .State ... " Varlık türü ClientContactViewModel, geçerli içerik için modelin bir parçası değildir. "
Bunu yapmayı nasıl yaparım? Üzgünüm, buna yeniyim. –
Güncellenmiş yanıtımı görün. – jrummell
Açık olmak gerekirse, benim görünüm modelimi yukarıda yazdığınız koda göre güncellemem gerekiyor mu? Yardım ettiğin için teşekkür ederim. –