Comboboxes in MVC from model driven ViewModel
(Created page with "To get everything to work as in the article you need the latest build (written 131108) Combobox, picklist or dropdown list – many names for the same thing. The combobox is...")
 
No edit summary
Line 24: Line 24:


In the View I enter this:
In the View I enter this:
<html>
<pre class="code"><span style="background: white; color: black">    </span><span style="background: white; color: blue">&lt;</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"&gt;
      </span><span style="background: yellow; color: black">@</span><span style="background: white; color: black">Html.DropDownListFor(x =&gt; 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">&lt;/</span><span style="background: white; color: maroon">div</span><span style="background: white; color: blue">&gt;
    &lt;</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"&gt;
            </span><span style="background: yellow; color: black">@</span><span style="background: white; color: black">Html.EditorFor(model =&gt; model.OwnedBy)    // Added this just for info 
    </span><span style="background: white; color: blue">&lt;/</span><span style="background: white; color: maroon">div</span><span style="background: white; color: blue">&gt;
</span></pre>
</html>


And I get this:
And I get this:

Revision as of 15:04, 6 November 2018

To get everything to work as in the article you need the latest build (written 131108)

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

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

When creating the viewmodel go like this (Look at the 2 last lines in the Green ViewModelClass and at the blue):

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

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

You get this:

Modlr suggests Example1.allinstances to 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.

I make sure CodeGen is checked and then generate code:

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

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:

    <div class="display-field">
      @Html.DropDownListFor(x => x.Example1_AsExternalId, 
        new SelectList(Model.Example1_PickList , //The list
                       "Identity",               // What attribute to pick
                       "Presentation",           // what to present
                       Model.Example1_AsExternalId)) // what to set on pick
    </div>
    <div class="display-field">
            @Html.EditorFor(model => model.OwnedBy)    // Added this just for info   
    </div>

And I get this:

Then I change in the combo and press save:

The save method needs to do nothing special to make this happen:

This page was edited 69 days ago on 02/10/2024. What links here