Note that there are some explanatory texts on larger screens.

plurals
  1. POasp.net mvc 3 Dropdown list with compound display member?
    primarykey
    data
    text
    <p>I have inherited a half completed MVC project which is to keep track of licenses for our customers' products.</p> <p>There are two dropdown lists on the license Create.cshtml page, the first of which allows you to choose a customer and the second of which then populates the products that customer owns (CustomerProducts) to allow you to select which CustomerProduct you wish to create a license for, as follows:</p> <pre><code>&lt;div class="editor-label"&gt;Customer&lt;/div&gt; &lt;div class="editor-field"&gt; @Html.DropDownListFor(model =&gt; model.SelectedCustomerId, new SelectList(Model.Customers, "Id", "Name"), "-- Select Customer --") @Html.ValidationMessageFor(model =&gt; model.SelectedCustomerId) &lt;/div&gt; &lt;div class="editor-label"&gt;Product&lt;/div&gt; &lt;div class="editor-field"&gt; @Html.DropDownListFor(model =&gt; model.SelectedCustomerProductId, Enumerable.Empty&lt;SelectListItem&gt;(), "-- Select Product --") @Html.ValidationMessageFor(model =&gt; model.SelectedCustomerProductId) &lt;/div&gt; </code></pre> <p>The problem I'm running into is that CustomerProducts are associated with both a Product <em>and</em> a Version but the dropdown is only displaying the Product name, not the Version name so if the customer owns both "Productname v1.0" and "Productname v1.1" the dropdown only shows Productname twice. So what I'd be looking for is something like (pseudocode):</p> <pre><code>@Html.DropDownListFor(model =&gt; model.SelectedCustomerProductId + " (" + model.SelectedCustomerProductId.ProductVersion.Name + ")", Enumerable.Empty&lt;SelectListItem&gt;(), "-- Select Product --") </code></pre> <p>I'm sure there must be a simple way to get that dropdown to display both the Product and the Version but I have scoured and scoured every source I can think of and haven't been able to come up with a solution.</p> <p>Apologies if this is an elementary question; I'm new to MVC and have spent a couple of days searching for a solution to what seems to me should be a very simple issue!</p> <p><strong>Edit:</strong></p> <p>Following up on @von v.'s suggestion below, added a read only property to CustomerProduct:</p> <pre><code>public virtual string ProductVersionFullName { get { return Product.Name + " (" + ProductVersion.Name + ")"; } } </code></pre> <p>It's then just a case of using that property as the display member rather that trying to bind to multiple properties of the CustomerProduct in the dropdown (the dropdown is populated by a method in LicensesController which is where the display member is set). I knew I was missing something simple!</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.
    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