Skip to main content

ListBoxExtension.GetSelectedValues<T>(String) Method

Returns an array of the values of the ListBox’s selected items.

Namespace: DevExpress.Web.Mvc

Assembly: DevExpress.Web.Mvc5.v24.2.dll

Declaration

public static T[] GetSelectedValues<T>(
    string name
)

Parameters

Name Type Description
name String

A string specifying the editor name.

Type Parameters

Name
T

Returns

Type Description
T[]

An array that contains zero or more T[] objects that are the values of the selected items.

Remarks

Use the GetSelectedValues<T> method to obtain an array of the values of the selected items within a ListBox.

Example

View code - “Index” (Razor):

@(Html.DevExpress().ListBox(settings => {
     settings.Name = "UserProgLanguage";
     settings.Properties.ValueField = "ID";
     settings.Properties.TextField = "Name";
     settings.Properties.ValueType = typeof(int);
     settings.Properties.SelectionMode = ListEditSelectionMode.CheckColumn;
     settings.Width = 200;
     settings.Height = 200;
     })
     .BindList(ViewData["ProgLanguageItems"])
     .GetHtml()
)

@Html.DevExpress().Button(settings => {
     settings.Name = "btnSubmit";
     settings.UseSubmitBehavior = true;
     settings.Text = "Submit";
}).GetHtml()

View code - “Success” (Razor):

<h2>Data is successufully submitted:</h2>
ProgLanguages:
<ul>
     @foreach (int item in Model.SelectedProgLanguages) {
          <li>@item</li>
     }
</ul>                     
@Html.ActionLink("Reload Demo", "Index")

Controller code - “HomeController”:

using DevExpress.Web.Mvc;
...
public class HomeController : Controller {
     public ActionResult Index() {
          MyModel model = new MyModel();
          ViewData["ProgLanguageItems"] = GetProgLanguageItems();
          return View(model);
     }

     [HttpPost]
     public ActionResult Index([ModelBinder(typeof(DevExpressEditorsBinder))] MyModel model) {
          model.SelectedProgLanguages = ListBoxExtension.GetSelectedValues<int>("UserProgLanguage");
          TempData["PostedModel"] = model;
          return RedirectToAction("Success");
     }

     public ActionResult Success() {
          return View(TempData["PostedModel"]);
     }

     private IList GetProgLanguageItems() {
          List<ListBoxItemObject> items = new List<ListBoxItemObject>();
          XDocument document = XDocument.Load(Server.MapPath("~/App_Data/ProgLanguages.xml"));
          foreach (var item in document.Root.Elements("ProgLanguage")) {
               items.Add(new ListBoxItemObject() {
                    ID = Convert.ToInt32(item.Attribute("ID").Value),
                    Name = (string)item.Attribute("Name").Value
               });
          }
          return items;
     }
}

public class MyModel {
     public int[] SelectedProgLanguages { get; set; }
}

public class ListBoxItemObject {
     public int ID { get; set; }
     public string Name { get; set; }
}

The image below shows the result

ListBoxExtension_GetSelectedValues

See Also