1

我是 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,但我看到的唯一示例是在传递下拉信息时使用而不是 viewbag。

我想我有几个问题:

  1. ViewModel 会像下面这样吗?

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

任何帮助将不胜感激,以帮助我解决这个问题...

如果联系人没有关联的电话号码,这是修改为支持的编辑(获取)

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


}

再次感谢你的帮助

4

2 回答 2

0

好的,试试这个:

电话号码

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

SaveContact:取决于您如何设置 Repositories 或 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. 然后 Scaffold aCreate Strongly Typed PhoneNumber Partial View to this folderPhoneNumber像它的模型一样命名它。

脚手架一个Create Strongly Typed Contact View名字吧Create

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

于 2013-04-14T00:31:19.720 回答
0

我认为您的视图模型应如下所示:

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
    };
于 2013-04-13T22:18:41.377 回答