This article will show you how you can bind mvc listboxfor control and
retrieve the selected value using c#.net. This article we can use in
MVC2,MVC3,MVC4,MVC5, MVC6.
Some of my previous articles are as follows: How
To Move All Items From One Listbox To Another In C#, Move
ListBox Item From one ListBox To another ListBox Using jQuery In Asp.Net, Display
User Detail Using jQuery ToolTip In Asp.Net MVC WebGrid, Transfer
Listbox Items to Another Listbox Using C#.Net and VB.Net | How to Move List Box
Items to another List Box in C#, Bind
and Retrieve ListBox Selected Value in MVC Using C#.Net.
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
namespace
MvcApplication8.Models
{
public class CountryModel
{
public int[] CountryId { get; set; }
public List<Country> CountryList {
get; set; }
}
public class Country
{
public int Id { get; set; }
public string CountryName { get; set; }
}
}
|
In above mode we have a catch. Please check this “public int[] CountryId { get; set; }”. This piece of code
will return the array of the selected item of the lisbox. If we not make the
array we will get the below error.
The parameter
'expression' must evaluate to an IEnumerable when multiple selection is
allowed.
Now we will add a controller class file and add the below
code.
using
MvcApplication8.Models;
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace
MvcApplication8.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
[HttpGet]
public ActionResult
Index()
{
CountryModel
countryModel = new CountryModel();
countryModel.CountryList = new List<Country>();
countryModel.CountryList = Country();
return
View(countryModel);
}
[HttpPost]
public ActionResult
Index(int[]
CountryId)
{
CountryModel
countryModel = new CountryModel();
countryModel.CountryList = new List<Country>();
countryModel.CountryList = Country();
//Seleced value
string countrySelected =
"";
foreach (int item in CountryId)
{
countrySelected = countrySelected + ",
" + item.ToString();
}
ViewBag.Country = countrySelected;
return
View(countryModel);
}
public List<Country> Country()
{
List<Country> countryList =
new List<Country>();
countryList.Add(new Country { Id = 1,
CountryName = "India" });
countryList.Add(new Country { Id = 2, CountryName = "Pakistan" });
countryList.Add(new Country { Id = 3, CountryName = "America" });
countryList.Add(new Country { Id = 4, CountryName = "Japan" });
return countryList;
}
}
}
|
In above code I have added the list of country with values,
and added get and post method on index. Here when post back will take place we will
the list of all the selected listbox items. Now we will create the view and add
the below code.
@model MvcApplication8.Models.CountryModel
@{
ViewBag.Title
= "MVC ListBoxFor Bind And Retrieve
Selected Value Using C#.Net";
}
@using (Html.BeginForm("Index", "Home"))
{
<h3>ListBox In MVC</h3>
@Html.ListBoxFor(s =>
s.CountryId, new SelectList(Model.CountryList, "Id", "CountryName"), new { @style = "width:200px;" })<br /><br />
<input type="submit" value="Submit" />
<br /><br />
<div>Selected Value: @ViewBag.Country</div>
}
|
In above code I have bond the country list to the listbox
control in mvc. Now we have done run the application and check the output.
0 comments:
Please let me know your view