Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET MVC Model properties not in view returning null
    primarykey
    data
    text
    <p>I have an EF Code First model that has an ICollection property like so.</p> <pre><code>public class AccountProfile { [Key] public int AccountID { get; set; } public string AccountName { get; set; } public string Email { get; set; } [Required] public virtual ICollection&lt;UserProfile&gt; LinkedUserProfiles { get; set; } } </code></pre> <p>I'm binding an edit view to this model, but it only shows the AccountName and Email properties.</p> <p>I have a HttpPost ActionResult to update the model that takes an AccountProfile.</p> <p>When posting, the AccountProfile object only has the AccountName and Email properties populated. The LinkedUserProfiles is null. This means that the model cannot be updated as the LinkedUserProfiles property is required.</p> <p>I have also tried something like the following without any luck</p> <pre><code> var curAccountProfile = _accountProfileRepository.GetById(accountProfile.AccountID); TryUpdateModel(curAccountProfile); </code></pre> <p>What am I doing wrong? How should this circumstance be handled in MVC?</p> <p><strong>UPDATE</strong> The data was coming from a repository eg.</p> <pre><code>var accountProfile = _accountProfileRepository.ById(1); return View(accountProfile); </code></pre> <p>Inspecting the accountProfile object before the view was loaded shows that the collection is being retrieved - so it's not a case of lazy loading not working as expected.</p> <p>I have since implemented AutoMapper, created a ViewModel for the view, and changed my code to be something like this:</p> <pre><code>var accountProfile = _accountProfileRepository.ById(accountInformation.AccountID); var result = _mappingEngine.Map&lt;DisplayAccountInformationViewModel, AccountProfile&gt;(accountInformation, accountProfile); _unitOfWork.Commit(); </code></pre> <p>It's working as expected now - but it seems like more effort than it should be?</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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