2017-04-26 61 views
-1

我正在使用實體框架核心來構建一個簡單的Web應用程序。對於這個應用程序,我創建了一個名爲Company的模型,其中包含基本業務信息+聯繫人列表(銷售代表)。允許用戶編輯MVC中的列表項目?

這裏是我的模型:

public class Company 
{ 
    [Key] 
    public int ID { get; set; } 
    public string Name { get; set; } 
    public string Promo { get; set; } 
    public virtual List<Contact> Contacts { get; set; }  
} 

public class Contact 
{ 
    [Key] 
    public int ContactID { get; set; } 
    [ForeignKey("Company")] 
    public int CompanyID { get; set; } 
    public virtual Company Company { get; set; } 
    public string ContactName { get; set; } 
    public string ContactNumber { get; set; } 
} 

這裏的控制器的index()方法:

// GET: Companies 
    public async Task<IActionResult> Index() 
    { 
     List<Company> viewModelData = await _context.Companies 
      .Include(c => c.Contacts) 
      .ToListAsync(); 
     return View(viewModelData); 
    } 

編輯方法:

// GET: Companies/Edit/5 
     public async Task<IActionResult> Edit(int? id) 
     { 
      if (id == null) 
      { 
       return NotFound(); 
      } 

      var company = await _context.Companies 
       .Include(v => v.Contacts) 
       .FirstOrDefaultAsync(m => m.ID == id); 
      if (company == null) 
      { 
       return NotFound(); 
      } 
      return View(company); 
     } 

     // POST: Companies/Edit/5 

     [HttpPost] 
     [ValidateAntiForgeryToken] 
     public async Task<IActionResult> Edit(int? id, [Bind("ID,Name,Promo,Contacts")] Company company) 
     { 
      if (id == null) 
      { 
       return NotFound(); 
      } 

      var companyToUpdate = await _context.Companies 
       .Include(v => v.Contacts) 
       .FirstOrDefaultAsync(m => m.ID == id); 
      if (await TryUpdateModelAsync<Company>(
       companyToUpdate, 
       "", 
       i => i.Name, i => i.Promo, i => i.Contacts 
       )) { 
       try 
       { 
        await _context.SaveChangesAsync(); 
       } 
       catch (DbUpdateException /* ex */) 
       { 
        //Log the error (uncomment ex variable name and write a log.) 
        ModelState.AddModelError("", "Unable to save changes. " + 
         "Try again, and if the problem persists, " + 
         "see your system administrator."); 
       } 
      return RedirectToAction("Index"); 
     } 
     return View(companyToUpdate); 
    } 

這是不正確的,因爲代碼只允許我編輯公司信息。如何修改代碼,以便我可以在同一個編輯視圖上編輯公司&的聯繫人?

+0

工作所以我不能評論,但不幸的是,但我可以建議使用視圖模型,而不是實際的Dto,然後將視圖模型從POST映射到Dto - 或使用AutoMapper來處理這些映射。然後,您只需在映射和保存後使用'_context.Companies.Update(companyToUpdate);'。 – ColinM

回答

1

如果你純粹是想更新值,那麼你可以像這樣明確地更新它們。視圖模型也是推薦的,但這可以歸結爲好的與壞的練習。這省略了異常處理,只作爲如何將這些值映射的例子,你就必須修改您的控制器的剩餘部分直接與CompanyEditViewModel

[HttpPost] 
[ValidateAntiForgeryToken] 
public async Task<IActionResult> Edit(int? id, [Bind("ID,Name,Promo,Contacts")] CompanyEditViewModel company) 
{ 
    if (!ModelState.IsValid) 
     return RedirectToAction("Index"); 

    var companyToUpdate = await _context.Companies 
     .Include(v => v.Contacts) 
     .FirstOrDefaultAsync(m => m.ID == id); 

    // Assign the new values 
    companyToUpdate.Name = company.Name; 
    companyToUpdate.Promo = company.Promo; 
    companyToUpdate.Contacts = company.Contacts?.ToList(); 

    // Update and save 
    _context.Companies.Update(companyToUpdate); 
    await _context.SaveChangesAsync(); 

    return View(companyToUpdate); 
} 

public class Company 
{ 
    public int ID { get; set; } 
    public string Name { get; set; } 
    public string Promo { get; set; } // Yes or No field 
    public List<Contact> Contacts { get; set; } 

    public class Contact 
    { 
     [Key] 
     public int ContactID { get; set; } 

     public int CompanyID { get; set; } 
     public string ContactName { get; set; } 
     public string ContactNumber { get; set; } 
    } 
} 

// The View Model contains the Company details which were modified 
// The first Edit method will have to be updated to bind this View Model to the view 
public class CompanyEditViewModel 
{ 
    public int ID { get; set; } 
    public string Name { get; set; } 
    public string Promo { get; set; } 
    public IList<Company.Contact> Contacts { get; set; } 
} 
我從來沒有實際使用`TryUpdateModel`
相關問題