Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I've been struggling with this same type of scenario and eventually came to realize that the underlying problem is that the MVC default model binder does not seem to work on EntitySet&lt;T&gt; fields, only List&lt;T&gt; fields. I did however find a simple workaround that seems acceptable. In my case, I have a Company entity that has one to many relationship to Contacts (my Linq-to-Sql EntitySet).</p> <p>Since it seems that when I change my code from EntitySet&lt;Contact&gt; to List&lt;Contact&gt;, the MVC default model binder starts working as expected (even though the LTS isn't now), I figured I would provide an alternate, "aliased" property to MVC that is of type List&lt;Contact&gt;, and sure enough, this seems to work.</p> <p>In my Company entity class:</p> <pre><code> // This is what LINQ-to-SQL will use: private EntitySet&lt;Contact&gt; _Contacts = new EntitySet&lt;Contact&gt;(); [Association(Storage="_Contacts", OtherKey="CompanyID", ThisKey="ID")] public EntitySet&lt;Contact&gt; Contacts { get { return _Contacts; } set { _Contacts.Assign(value); } } // This is what MVC default model binder (and my View) will use: public List&lt;Contact&gt; MvcContacts { get { return _Contacts.ToList&lt;Contact&gt;(); } set { _Contacts.AddRange(value); } } </code></pre> <p>So now, in my View, I have the following:</p> <p><code><pre> &lt;label&gt;First Name* &lt;%= Html.TextBox("Company.MvcContacts[" + i + "].FirstName") %&gt; &lt;/label&gt; &lt;label&gt;Last Name* &lt;%= Html.TextBox("Company.MvcContacts[" + i + "].LastName") %&gt; &lt;/label&gt; </pre></code></p> <p>Seems to work like a charm!</p> <p>Best of luck! -Mike</p>
 

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