Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Okay, try this:</p> <p><strong>PhoneNumber</strong></p> <pre><code>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; } } </code></pre> <p><strong>Contact</strong></p> <pre><code>public class Contact { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int ContactId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public List&lt;PhoneNumber&gt; PhoneNumbers { get; set; } } </code></pre> <p><strong>SaveContact</strong>: depending on how you have set up your Repositories or Ef class. This can either go in your EfRepository implemention or your EfDb class.</p> <pre><code>public void SavePlayer(Contact contact) { using (var context = new EfDb()) { if (contact.ContactId == 0) { context.Contacts.Add(contact); } else if (contact.ContactId &gt; 0) { var currentContact = context.Contacts .Include(c =&gt; c.PhoneNumber) .Single(c =&gt; c.ContactId== contact.ContactId); context.Entry(currentContact).CurrentValues.SetValues(contact); currentContact.PhoneNumber= contact.PhoneNumber; } context.SaveChanges(); } } </code></pre> <p><strong>Edit action</strong></p> <pre><code> [HttpGet] public ActionResult Edit(int id) { var contact= _dataSource.Contacts.FirstOrDefault(c =&gt; 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); } </code></pre> <p><strong>Views</strong></p> <p>In your <code>Contact View Folder</code> add <code>EditorTemplates folder</code>. Then Scaffold a <code>Create Strongly Typed PhoneNumber Partial View to this folder</code> and name it <code>PhoneNumber</code> like its model.</p> <p>Scaffold a <code>Create Strongly Typed Contact View</code> name it <code>Create</code></p> <p>Then add <code>@Html.EditorFor(model =&gt; model.PhoneNumber)</code> to the master Create View.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload