在一个视图中编辑/列出多个模型



我是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; }
    }
}

Contact和PhoneNumber之间的关系是一对多,但是我希望能够在Primary设置为true时编辑first_name, last_name和Number,因此我们将每个联系人记录只编辑一个电话号码。

我看到过类似的帖子,指出使用ViewModel,但我所见过的ViewModel的唯一例子是在传递下拉信息时使用viewbag而不是viewbag。

我想我有几个问题:

  1. ViewModel看起来像下面吗?

    public class ContactPrimaryNumberViewModel
    {
        public Contact ContactToEdit {get; set;}
        public PhoneNumber PhoneNumberToEdit {get;set;}
    }
    
  2. 编辑(post)和编辑(get)会是什么样子?

如果你能帮我解决这个问题,我将不胜感激。

这里是Edit(get)修改以支持如果联系人没有关联的电话号码

'//GET:/Contact/Edit/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; }

}

再次感谢您的帮助

我认为你的视图模型应该是这样的:

public class ContactPrimaryNumberViewModel
{
    public int ID { get; set; }
    public string First_Name { get; set; }
    public string Last_Name { get; set; }
    public string Number { get; set; }
}

你的更新看起来像这样:

Contact c = context.Contacts.Find(id);
PhoneNumber p = context.PhoneNumbers
    .FirstOrDefault(x => x.id == id && x.Primary == true);
//validate input
//update as necessary
//SaveChanges() etc...

从你的评论-你新建了模型类ContactPrimaryNumberViewModel:

var 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
    };

好的,试试这个:

PhoneNumber

public class PhoneNumber
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public string Number { get; set; }
    public bool Primary { get; set; }
    [ForeignKey("Contact"), DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int? ContactId { get; set; }
    public virtual Contact Contact { get; set; }
}

public class Contact
{  
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ContactId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public List<PhoneNumber> PhoneNumbers { get; set; }
}

saveccontact :取决于您如何设置您的repository或Ef类。这可以放在你的EfRepository实现或EfDb类中。

public void SavePlayer(Contact contact)
    {
        using (var context = new EfDb())
        {                
            if (contact.ContactId == 0)
            {                    
                context.Contacts.Add(contact);
            }
            else if (contact.ContactId > 0)
            {
                var currentContact = context.Contacts
                    .Include(c => c.PhoneNumber)
                    .Single(c => c.ContactId== contact.ContactId);
                context.Entry(currentContact).CurrentValues.SetValues(contact);
                currentContact.PhoneNumber= contact.PhoneNumber;
            }
            context.SaveChanges();
        }
    }

编辑动作

    [HttpGet]
    public ActionResult Edit(int id)
    {
        var contact= _dataSource.Contacts.FirstOrDefault(c => c.Id == id);
        return View(player);
    }
    [HttpPost]
    public ActionResult Edit(Contact contact)
    {
        try
        {
            if (ModelState.IsValid)
            {
                _dataSource.SaveContact(contact);
                return RedirectToAction("About", "Home");
            }
        }
        catch (Exception)
        {
            ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator.");
        }            
        return View(contact);
    }

在你的Contact View Folder中添加EditorTemplates folder。然后支架Create Strongly Typed PhoneNumber Partial View to this folder并将其命名为PhoneNumber,就像它的模型一样。

Create Strongly Typed Contact View命名为Create

然后将@Html.EditorFor(model => model.PhoneNumber)添加到主创建视图

相关内容

  • 没有找到相关文章

最新更新