Sunday, 14 August 2016

MVC ListBoxFor Bind And Retrieve Selected Value Using C#.Net

8/14/2016 - By Pranav Singh 0

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.


So for this article first we will create a new mvc application and add a class file in model folder.

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.


About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top