---
title: "ListBox control in MVC"  
description: "Here In this article, I am explaining how to add items in ListBox and how to get selected items from ListBox in ASP.NET MVC  Create a property in a"  
author: "Chris Anderson"  
published: 2011-10-04  
updated: 2020-03-04  
canonical: https://www.mindstick.com/articles/761/listbox-control-in-mvc  
category: "asp.net mvc"  
tags: ["asp.net mvc"]  
reading_time: 4 minutes  

---

# ListBox control in MVC

Here In this article, I am explaining how to [add items](https://www.mindstick.com/forum/160381/how-to-add-items-to-a-list-collection-in-c-sharp) in ListBox and how to get [selected items](https://www.mindstick.com/forum/352/i-need-to-display-my-checkboxlist-selected-items-in-a-label-how-to-do) from ListBox in [**ASP.NET MVC**](https://www.mindstick.com/articles/776/login-and-registration-form-in-asp-dot-net-mvc).\

Create a [property](https://www.mindstick.com/articles/198559/an-ownership-of-property-in-hua-hin) in a [model class](https://www.mindstick.com/forum/2281/filter-objects-inside-model-class-by-date) that returns a country list to a caller. In the below model class I had created a property named Countries that return a list of countries.

```
using System.Collections.Generic;
using System.Web.WebPages.Html;
using System.ComponentModel.DataAnnotations;

namespace ListBoxMVC.Models{
     public class Country    {
         private List<SelectListItem> _countries = new List<SelectListItem>();
         [Required(ErrorMessage = "Please select a country")]         public string SelectedCountry { get;  set; }
         public List<SelectListItem> Countries         {
            get            {
              _countries.Add(new SelectListItem() { Text = "India", Value =  "India" });
              _countries.Add(new SelectListItem() { Text = "USA", Value =  "USA" });
              _countries.Add(new SelectListItem() { Text = "Russia", Value =  "Russia" });
              return _countries;
            }
         }
    }
}
```

\
In a [Controller](https://www.mindstick.com/blog/273/passing-values-from-controller-to-view-in-asp-dot-net-mvc) class, Index action returns a model class object to view. Like in the below Controller I passed the model class object to both the Index and GetValue View;

```
using System.Web.Mvc;
using ListBoxMVC.Models;
namespace ListBoxMVC.Controllers{
     public class HomeController :  Controller    {
         public ActionResult Index()         {
            return View(new Country());         }
         public ActionResult GetValue(Country model)         {
            return View(model);
         }
    }
}
```

In the below Index view, I have created a ListBox and use a Model property to fill the items in ListBox. I also show that how to call [javascript](https://www.mindstick.com/articles/874/how-to-create-watermark-text-for-textbox-by-using-javascript) method through onchange event of ListBox (onchange="selectedIndexChanged()"). When you click on the [submit button](https://www.mindstick.com/forum/130/its-possible-without-submit-button) it will call another view (GetValue) and show the [selected value](https://www.mindstick.com/forum/525/get-selected-value-of-datagridviewcomboboxcolumn) of ListBox. \

```
 <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<ListBoxMVC.Models.Country>" %>
<!DOCTYPE html>
<html>
<head id="Head1" runat="server">
     <title>Index</title>
         <script type="text/javascript">
            function selectedIndexChanged() {
            }
         </script>
</head>
<body>
     <% using (Html.BeginForm("GetValue","Home",FormMethod.Post))
    {%>
     <div>
         Select a product : <br /><br /><%: Html.ListBoxFor(x => x.SelectedCountry,
                                   new  SelectList(Model.Countries, "Value""Text"),
                  new { id =  "listBox1" , onchange =  "selectedIndexChanged()" })%>
         <%: Html.ValidationMessageFor(x => x.SelectedCountry) %><br /><br />
         <input type="submit" value="Submit" />
     </div>
     <% } %>
</body>
</html>
```

In the GetValue View, I had used a Model property to show the selected ListBox value.

```
 <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<ListBoxMVC.Models.Country>" %>
<!DOCTYPE html>
<html>
<head runat="server">
     <title>GetValue</title>
</head>
<body>
     <div>
         The selected country is: <b><%: ViewData.Model.SelectedCountry %></b>
     </div>
</body>
</html>
```

After completing the above process you can see an output of [your project](https://www.mindstick.com/forum/156583/best-way-to-link-bootstrap-file-in-your-project) by pressing F5. In the browser, it will show you the list of country in a ListBox.

![ListBox control in MVC](https://www.mindstick.com/mindstickarticle/09ccfe6f-c6c1-43d4-a801-d2cadecc380b/images/1480a066-36db-4098-86be-e6fbf86dfc90.png)

When you select a country and click on the submit button, it will show the selected country in the GetValue view.

![ListBox control in MVC](https://www.mindstick.com/mindstickarticle/09ccfe6f-c6c1-43d4-a801-d2cadecc380b/images/8ee2bdfe-1ec2-478b-84ea-99141f656e19.png)

By using the above process you can simply learn how to add items in the ListBox and get selected value from the ListBox in MVC.

---

Original Source: https://www.mindstick.com/articles/761/listbox-control-in-mvc

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
