Note that there are some explanatory texts on larger screens.

plurals
  1. POIs my Linq-SQL breaking my MVC2 model binding?
    primarykey
    data
    text
    <p>I am trying to handle the http-post from a web form using MVC 2s built in model binding. From what I have been searching through for the past few hours I have figured it is a bit finicky with "objects within objects." </p> <p>I'm looking for any kind of answers or links to resources that may help me figure this out. I'm still in the early stages of this also so if there is a better way - I'm all ears. I have been writing the Linq-SQL myself and not using the code generation. I think the closest answer I'm looking for is <a href="https://stackoverflow.com/questions/4273443/asp-net-mvc2-linq-to-sql-relationship-entities-select-list-model-binding">here</a>, but I'm still not getting it.</p> <p>Model Client (my best guess for where the issue is):</p> <pre><code>public class Client { public int ClientID { get; set; } ... [Column(Name = "Address_id")] internal int AddressID { get; set; } internal EntityRef&lt;Address&gt; _address; [System.Data.Linq.Mapping.Association(ThisKey = "AddressID", Storage = "_address")] public Address Address { get { return _address.Entity; } internal set { _address.Entity = value; AddressID = value.AddressID; } } } </code></pre> <p>Model Address (within client entity)</p> <pre><code>public class Address { [Column] public string Address1 { get; set; } [Column] public string Address2 { get; set; } [Column] public string City { get; set; } ... </code></pre> <p>View Model:</p> <pre><code> public class ClientFormViewModel { public Client Client { get; set; } ... } </code></pre> <p>View:</p> <pre><code> &lt;!-- id is hidden --&gt; &lt;%: Html.EditorFor(m =&gt; m.Client.ClientID) %&gt; ... &lt;%: Html.EditorFor(m =&gt; m.Client.Address.AddressID) %&gt; &lt;%: Html.LabelFor(m =&gt; m.Client.Address.Address1) %&gt; &lt;%: Html.EditorFor(m =&gt; m.Client.Address.Address1) %&gt;&lt;br /&gt; &lt;%: Html.LabelFor(m =&gt; m.Client.Address.Address2) %&gt; &lt;%: Html.EditorFor(m =&gt; m.Client.Address.Address2) %&gt;&lt;br /&gt; ... </code></pre> <p>Controller:</p> <pre><code> public ViewResult Edit(int clientId) { var client = clientsRepository.Clients.First(x =&gt; x.ClientID == clientId); ... // create view model var viewModel = new ClientFormViewModel { Client = client, ... }; return View(viewModel); } [HttpPost] public ActionResult Edit(ClientFormViewModel clientForm) { if (ModelState.IsValid) { clientsRepository.SaveClient(clientForm.Client); return RedirectToAction("List"); } else // validation error, so redisplay the same view ... } </code></pre> <p>So my issue is... when I get into the HttpPost action, the <em>clientForm.Client.Address</em> is always <em>null</em>. Although, when I look into the <em>ModelState</em> (which is valid), or use <em>Request.["key"]</em>, all of the keys match the structure of my object. For example, I see <em>ModelState["Client.Address.Address1"]</em>, <em>"Client.Address.Address2"</em>, etc.</p> <p>All other basic properties are filled in fine, which make me think the linq-sql code is breaking the model binding. But how? And is there a way to fix it? If those keys are within the Request/ModelState dictionary, why are they not being mapped to the object? Am I totally missing something obvious?</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.
 

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