MVC 5 Viewmodel and multiple tables
<p>I've just finished 2 views, an insert and a index view and I have to say, it's not easy dealing with multiple tables. The &quot;insert&quot; was the easiest, I just had to add to 2 different tables:</p>
<pre><code>[HttpPost]
public ActionResult Index(CustPlayerVM custPlayer)
{
    var Cust = new Cust()
    {
        CustID = custPlayer.CustID,
        Color = custPlayer.Color
    };
    var Player = new Player()
    {
        PlayerName = custPlayer.PlayerName
    };
    db.Custs.Add(Cust);
    db.Players.Add(Player);
    db.SaveChanges();
    return View();
}
</code></pre>
<p>The problem is that I had ALL kinds of errors with <code>List&lt;&gt;</code> and <code>IEnumerable&lt;&gt;</code> trying to code the view. Either you get the dreaded &quot;model item passed into the dictionary is of type&quot; error or the null reference in the view when the &quot;foreach&quot; loop was processed. It was a miracle that this worked, but I can't tell you why. That's why I posted the question to begin with.</p>
<p>Here's what the &quot;view&quot; looks like:</p>
<pre><code>public ActionResult List()
{
    ViewBag.Message = &quot;custPlayer&quot;;
    //CustPlayerVM vm = new CustPlayerVM();      

    List&lt;object&gt; vm = new List&lt;object&gt;();
    vm.Add(db.Custs.ToList());
    vm.Add(db.Players.ToList());          
    ViewBag.PlayerCnt = db.Players.Count();
    return View(vm);
}
</code></pre>
<p>Notice the <code>List&lt;object&gt;</code> (as opposed to view model that's commented out).</p>
<p>And this is the view:</p>
<pre><code>@model IEnumerable&lt;object&gt; @{ 
    List&lt;viewModelA.Cust&gt; lstCust = Model.ToList()[0] as List&lt;viewModelA.Cust&gt;;
    List&lt;viewModelA.Player&gt; lstPlayer = Model.ToList()[1] as List&lt;viewModelA.Player&gt;; }   

@Html.ActionLink(&quot;Create New&quot;, &quot;Create&quot;) &lt;/p&gt; 
&lt;table class=&quot;table&quot;&gt;
    &lt;tr&gt;
        &lt;th&gt;
            @Html.Raw(&quot;Color&quot;)
        &lt;/th&gt;
        &lt;th&gt;
            @Html.Raw(&quot;CustID&quot;)
        &lt;/th&gt;
        &lt;th&gt;
            @Html.Raw(&quot;ID&quot;)
        &lt;/th&gt;
        &lt;th&gt;
            @Html.Raw(&quot;Player Name&quot;)
        &lt;/th&gt;
        &lt;th&gt;
            @Html.Raw(&quot;PlayerID&quot;)
        &lt;/th&gt;
    &lt;/tr&gt;
    &amp;nbsp;
    @for (int i = 0; i &lt; ViewBag.PlayerCnt; i++)
    {
        &lt;tr&gt;
            &lt;td&gt;@lstCust[i].Color&lt;/td&gt;
            &lt;td&gt;  @lstCust[i].CustID&lt;/td&gt;
            &lt;td&gt;  @lstCust[i].ID &lt;/td&gt;
            &lt;td&gt;   @lstPlayer[i].PlayerName  &lt;/td&gt;
            &lt;td&gt; @lstPlayer[i].ID&lt;/td&gt;
        &lt;/tr&gt;
    } 
&lt;/table&gt;
</code></pre>
<p>I had to wrap a for loop around the code because the foreach ran for each table separately.</p>
<p>In the future, how are you supposed to deal with <code>IEnumerable</code> vs anonymous types? It shouldn't be this hard, correct?</p>