Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>An approach you could take is to extract the drop down list values into a viewmodel of their own. So:</p> <p><strong>Step 1:</strong> Create a view model (<code>ItemsViewModel</code>) that encapsulates the drop down list items:</p> <pre><code>using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Web; using System.Web.Mvc; namespace Models { public class DropDownListItem { public string Text { get; set; } public string Value { get; set; } } public class ItemsViewModel { private readonly List&lt;DropDownListItem&gt; _items; // The selected item: public string SelectedItem { get; set; } // The items: public IEnumerable&lt;SelectListItem&gt; Items { get { var allItems = _items.Select(i =&gt; new SelectListItem { Value = i.Value, Text = i.Text }); return DefaultItem.Concat(allItems); } } // Default item (i.e. the "select" text) if none selected public IEnumerable&lt;SelectListItem&gt; DefaultItem { get { return Enumerable.Repeat(new SelectListItem { Value = "-1", Text = "Select an item" }, count: 1); } } public ItemsViewModel() { } // Constructor taking in items from service and selected item string: public ItemsViewModel(List&lt;DropDownListItem&gt; items, string selected) { _items = items; SelectedItem = selected; } } } </code></pre> <p><strong>Step 2:</strong> Create a partial view in the Views folder that binds to the <code>ItemsViewModel</code>:</p> <pre><code>@model Models.ItemsViewModel @Html.DropDownListFor(m =&gt; m.SelectedItem, Model.Items) </code></pre> <p><strong>Step 3:</strong> In the appropriate controller (e.g. HomeController), place the child action that pulls the data from the service, the view model and the partial view together:</p> <pre><code>[ChildActionOnly] public ActionResult DropDownList(string type, string selected) { // If you need to call different services based on the type (e.g. Country), then pass through "type" and base the call on that var items = new ItemsViewModel( (from g in _service.getTitles() select new DropDownListItem { Text = g.Text, Value = g.Value }).ToList(), selected); return PartialView("DropDownPartial", items); } </code></pre> <p><strong>Step 4:</strong> Drop this line of code into the view where you need the drop down list:</p> <pre><code>@Html.Action("DropDownList", "Home", new { selected = "2", type = "country" }) </code></pre> <p>Note that <code>selected</code> and <code>type</code> are to be determined whichever way you see fit and are optional.</p> <p>Hopefully this gives you some inspiration.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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