🚀 Welcome to MDriven Learn –  MDriven is now on Discord!  Don’t miss the latest Release Notes.
Parsed/Comboboxes in MVC from model driven ViewModel
This page was created by PageReplicator on 2025-09-01. Last edited by PageReplicator on 2025-09-01.

Template:Notice



❗🕜 Warning: this article may contain outdated information. Consider before using any descriptions/solutions, otherwise, it still can be helpful. <a href="https://wiki.mdriven.net/index.php/Documentation:MDriven_Product_Line#Synonyms_and_name_changes"> Help: Synonyms and name changes </a>

To get everything to work as described in the article, you must have the latest build (written 131108).

Combobox, picklist, or dropdown list – many names apply to the same thing.

The Combobox is perfect when setting references between objects – if the number of items to pick from is not too large.

In the ViewModel Editor, you can decide how, and if, to handle <a href="/Documentation:How_Null_is_represented_in_your_picklist" title="Documentation:How Null is represented in your picklist" data-bs-title="Documentation:How_Null_is_represented_in_your_picklist">How Null is represented in your picklist</a>.

When creating the ViewModel, do this (look at the 2 last lines in the green ViewModelClass and the blue):

<a href="/File:Comboboxes_-_1.png" class="image" data-bs-title="File:Comboboxes_-_1.png" data-bs-filetimestamp="20181106150629"><img alt="Comboboxes - 1.png" src="/images/thumb/0/0a/Comboboxes_-_1.png/354px-Comboboxes_-_1.png" decoding="async" width="354" height="173" class="thumbborder" srcset="/images/0/0a/Comboboxes_-_1.png 1.5x" /></a>

This is automated for you, if you want, by right-clicking:

<a href="/File:Comboboxes_-_2.png" class="image" data-bs-title="File:Comboboxes_-_2.png" data-bs-filetimestamp="20181106150658"><img alt="Comboboxes - 2.png" src="/images/thumb/e/e5/Comboboxes_-_2.png/357px-Comboboxes_-_2.png" decoding="async" width="357" height="272" class="thumbborder" srcset="/images/thumb/e/e5/Comboboxes_-_2.png/536px-Comboboxes_-_2.png 1.5x, /images/e/e5/Comboboxes_-_2.png 2x" /></a>

Modlr knows that “Example1” is a perfect candidate for a Combobox since it is a single link.

You get this:

<a href="/File:Comboboxes_-_3.png" class="image" data-bs-title="File:Comboboxes_-_3.png" data-bs-filetimestamp="20181106150726"><img alt="Comboboxes - 3.png" src="/images/thumb/4/46/Comboboxes_-_3.png/362px-Comboboxes_-_3.png" decoding="async" width="362" height="203" class="thumbborder" srcset="/images/4/46/Comboboxes_-_3.png 1.5x" /></a>

Modlr suggests Example1.allinstances be the pick list, but you are free to change it. Maybe you want to add a “->orderby(e|e.attribute1)” there.

To get the whole MVC roundtrip to work, we need to add an identity column to the PickListPresentation class.

<a href="/File:Comboboxes_-_4.png" class="image" data-bs-title="File:Comboboxes_-_4.png" data-bs-filetimestamp="20181106150814"><img alt="Comboboxes - 4.png" src="/images/thumb/0/0b/Comboboxes_-_4.png/300px-Comboboxes_-_4.png" decoding="async" width="300" height="97" class="thumbborder" srcset="/images/0/0b/Comboboxes_-_4.png 1.5x" /></a>

Make sure CodeGen is checked and then generate code:

<a href="/File:Comboboxes_-_5.png" class="image" data-bs-title="File:Comboboxes_-_5.png" data-bs-filetimestamp="20181106150840"><img alt="Comboboxes - 5.png" src="/images/thumb/9/96/Comboboxes_-_5.png/448px-Comboboxes_-_5.png" decoding="async" width="448" height="218" class="thumbborder" srcset="/images/9/96/Comboboxes_-_5.png 1.5x" /></a>

Looking at the generated code for the ViewModel VMExample2, we see this new thing:

<a href="/File:Comboboxes_-_6.png" class="image" data-bs-title="File:Comboboxes_-_6.png" data-bs-filetimestamp="20181106150910"><img alt="Comboboxes - 6.png" src="/images/thumb/2/2e/Comboboxes_-_6.png/300px-Comboboxes_-_6.png" decoding="async" width="300" height="77" class="thumbborder" srcset="/images/2/2e/Comboboxes_-_6.png 1.5x" /></a>

Notice that modlr did not only add code for Example1, it also added code for Example1_AsExternalId… And this is important for MVC.

In the View, I enter this:


<span style="background: white; color: black">    </span><span style="background: white; color: blue"><</span><span style="background: white; color: maroon">div </span><span style="background: white; color: red">class</span><span style="background: white; color: blue">="display-field">
      </span><span style="background: yellow; color: black">@</span><span style="background: white; color: black">Html.DropDownListFor(x => x.Example1_AsExternalId, 
        </span><span style="background: white; color: blue">new </span><span style="background: white; color: #2b91af">SelectList</span><span style="background: white; color: black">(Model.Example1_PickList , </span><span style="background: white; color: green">//The list
                       </span><span style="background: white; color: #a31515">"Identity"</span><span style="background: white; color: black">,               </span><span style="background: white; color: green">// What attribute to pick
                       </span><span style="background: white; color: #a31515">"Presentation"</span><span style="background: white; color: black">,           </span><span style="background: white; color: green">// what to present
                       </span><span style="background: white; color: black">Model.Example1_AsExternalId)) // what to set on pick
    </span><span style="background: white; color: blue"></</span><span style="background: white; color: maroon">div</span><span style="background: white; color: blue">>
    <</span><span style="background: white; color: maroon">div </span><span style="background: white; color: red">class</span><span style="background: white; color: blue">="display-field">
            </span><span style="background: yellow; color: black">@</span><span style="background: white; color: black">Html.EditorFor(model => model.OwnedBy)    // Added this just for info   
    </span><span style="background: white; color: blue"></</span><span style="background: white; color: maroon">div</span><span style="background: white; color: blue">>
</span>


And I get this:

<a href="/File:Comboboxes_-_7.png" class="image" data-bs-title="File:Comboboxes_-_7.png" data-bs-filetimestamp="20181106150935"><img alt="Comboboxes - 7.png" src="/images/c/cd/Comboboxes_-_7.png" decoding="async" width="232" height="244" class="thumbborder" /></a>

Then I change the combo and press save:

<a href="/File:Comboboxes_-_8.png" class="image" data-bs-title="File:Comboboxes_-_8.png" data-bs-filetimestamp="20181106150957"><img alt="Comboboxes - 8.png" src="/images/0/0b/Comboboxes_-_8.png" decoding="async" width="239" height="244" class="thumbborder" /></a>

The save method does nothing special to make this happen:

<span style="background: white; color: black">        [</span><span style="background: white; color: #2b91af">HttpPost</span><span style="background: white; color: black">]
        </span><span style="background: white; color: blue">public </span><span style="background: white; color: #2b91af">ActionResult </span><span style="background: white; color: black">Details(</span><span style="background: white; color: blue">string </span><span style="background: white; color: black">Identity, </span><span style="background: white; color: #2b91af">VMExample2 </span><span style="background: white; color: black">offlinevm)
        {
          </span><span style="background: white; color: #2b91af">Example2 </span><span style="background: white; color: black">e2 = EcoSpace.ExternalIds.ObjectForId(Identity).AsObject </span><span style="background: white; color: blue">as </span><span style="background: white; color: #2b91af">Example2</span><span style="background: white; color: black">;
          </span><span style="background: white; color: blue">try
          </span><span style="background: white; color: black">{
            </span><span style="background: white; color: #2b91af">VMExample2 </span><span style="background: white; color: black">onlinevm=</span><span style="background: white; color: #2b91af">VMExample2</span><span style="background: white; color: black">.Create(EcoSpace, e2);
            </span><span style="background: white; color: #2b91af">ViewModelHelper</span><span style="background: white; color: black">.ApplyValues(offlinevm, onlinevm, </span><span style="background: white; color: blue">null</span><span style="background: white; color: black">);
            Commit();
            </span><span style="background: white; color: blue">return </span><span style="background: white; color: black">View(</span><span style="background: white; color: #a31515">"Details"</span><span style="background: white; color: black">, onlinevm);
          }
          </span><span style="background: white; color: blue">catch
          </span><span style="background: white; color: black">{
            </span><span style="background: white; color: blue">return </span><span style="background: white; color: black">View(</span><span style="background: white; color: #a31515">"Details"</span><span style="background: white; color: black">, offlinevm);
          }
        }
</span>