Note that there are some explanatory texts on larger screens.

plurals
  1. POList Properties and Controller Actions for Creation and Editting
    primarykey
    data
    text
    <p><em>First of all, please help me by telling where the question is not clear, as am not used to ask questions off the internet.</em></p> <p><strong>Problem in a Nutshell</strong>:<br> The framework makes it very easy to create editors for a viewmodel with simple scalar properties. However it does not scale well when we want to edit viewmodels that have lists of other objects. The framework should not assume the availability of a session neither javascript.</p> <p><strong>Code Sample</strong><br> I posted all the meaningful code here, which should give much better context than describing the problem with words. <em>I kept it as simple as possible</em>:</p> <pre><code>namespace Simple{ class SimpleController{ public Html CreateA(){ return View(new A()); } // this is usually an http post responding to a button under the displayed listing of Bs in the main form [HttpPost] public Html CreateA(A a, FormCollection collection){ // *** This is only necessary in the case of lists *** // *** what happens in the "MoreInvolved" case is // terrifying. I didn't post any attempt. *** if(collection["button"] == "AddB"){ a.listB.Add(new B()); return View(a); } else{ // save a to database // return redirect either to list or to details of created object } } } class A { public string a1; public string a2; public string a3; public List&lt;B&gt; listB; } class B { public string b1; public string b2; } } namespace MoreInvolved { class A { public string a1; public string a2; public string a3; public List&lt;B&gt; listB; } class B { public string b1; public string b2; public List&lt;C&gt; listC; } class C { public string c1; public string c2; } } </code></pre> <p><em>The views are not included in the code for the simple case, but it has been tested and it works well. I used expression trees for that matter to make it easier, but that's not posted in the code. However, in the more involved case, I have not completed, and I have not yet envisioned a simple solution other than pondering the bizarre and strange</em></p> <p>In the code above, the simple case is working as I expect. I can add <code>B</code>'s to an <code>A</code> without using any session or Javascript. However I had to add a sub-route in the controller's <code>CreateA()</code> to check when I've actually pressed an add button. In the more involved scenario, even more changes to the controller become necessary. The subroute will have to decide which <code>B</code> a new <code>C</code> needs to be added to.</p> <p>It shows two cases, one of which I have tested to work well. Do assume that the view used returns html that once posted gets bound back into deep objects.</p> <p><strong>The Problem</strong><br> Notice, in the code, how once we are in the more involved case, the programmer is immediately stomped with how a new object should be inserted when dealing with a second level child. <em>Specifically</em>, I don't want to have an if condition to add a <code>C</code> into a specific <code>B</code> within the controller for <code>A</code>. It's annoying if I had to write the same code if <code>List&lt;B&gt;</code> existed under another parent, say <code>D</code>.</p> <p><strong>Is the control model totally obliterated from MVC, doesn't that mean some things are extremely harder to do?</strong><br> I believe I am flirting with the need for user controls and some way to create controls that are reusable so that I can have a controller for <code>B</code> with its children (<code>C</code>'s) then reuse all of that when implementing a CRUD interface for <code>A</code>.</p> <p><strong>There's an imbalance between the ease of creating editors for scalar types, or an object consisting of scalar types on one hand and editors for lists of objects on the other</strong> The real issue is, there are automatic editors for scalar types, but none for lists of them. The solutions that I came up with are redundant, and introduce seemingly unnecessary additional code and headache.</p> <p><em>Final Notes: The reason am going all the trouble is that am trying to see how far I can push statelessness and keeping the code clean and within the limits of the MVC pattern. I wish there were more examples of places where a pattern fails rather than all the marketing like material on patterns. That way we can immediately spot where and where not to use certain pattern oriented products. I believe, regular ASP.NET would deal with my case here with a breeze using its viewstate and event flow model.</em></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