Monday, 23 June 2014

Bind and Retrieve ListBox Selected Value in MVC Using C#.Net

6/23/2014 - By Pranav Singh 0

In this article I will show you how you can bind a liatbox control in an mvc application using C#.Net and VB.Net.In this I have used @html. ListBox control, C#, controller, model, Entity Framework, Linq query.

In previous article I have show you how to Bind DropDownList Using Entity Framework in ASP.Net MVC Using C#.


So for this article first we will create a new asp.net mvc application and ads model file in it. After creating model file add the below code in it.

Model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace bIND_Dropdownin_MVC.Models
{
    public class CountryModel
    {
        public SelectList CountryListModel { get; set; }
    }
}

After creating model we will create the entity model.




Now we will create controller.  After creating controller we will create action view.

Controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using bIND_Dropdownin_MVC.Models;

namespace bIND_Dropdownin_MVC.Controllers
{
    public class HomeController : Controller
    {
        //Bind/Populate DropDownList Using Entity Framework in ASP.Net MVC Using C#
        public ActionResult Index()
        {
            /*Create instance of entity model*/
            NorthwindEntities objentity = new NorthwindEntities();
            /*Getting data from database*/
            List<Country> objcountrylist = (from data in objentity.Countries
                                            select data).ToList();
            SelectList objmodeldata = new SelectList(objcountrylist, "Id", "CountryName", 0);
            /*Assign value to model*/
            CountryModel objcountrymodel = new CountryModel();
            objcountrymodel.CountryListModel = objmodeldata;
            return View(objcountrymodel);
        }
        [HttpPost]
        public ActionResult Index(string listcountry)
        {
            /*Create instance of entity model*/
            NorthwindEntities objentity = new NorthwindEntities();
            /*Getting data from database*/
            List<Country> objcountrylist = (from data in objentity.Countries
                                            select data).ToList();
            Country objcountry = new Country();
            objcountry.CountryName = "Select";
            objcountry.Id = 0;
            objcountrylist.Insert(0, objcountry);
            SelectList objmodeldata = new SelectList(objcountrylist, "Id", "CountryName", 0);
            /*Assign value to model*/
            CountryModel objcountrymodel = new CountryModel();
            objcountrymodel.CountryListModel = objmodeldata;

            ViewBag.CountryName = objcountrylist.Where(m => m.Id ==      
            listcountry).FirstOrDefault().CountryName;

            return View(objcountrymodel);
        }

    }
}

In above code first we have created instance for entity model. After that we have used linq query to retrieve data from the table.

Now we will move to our view. In this we will add the below code

View
@model bIND_Dropdownin_MVC.Models.CountryModel
@{
    ViewBag.Title = "Bind ListBox in MVC Using C#.Net";
}
@using (Html.BeginForm("Index", "Home"))
{
    <h2>
        Bind ListBox</h2>
    @Html.ListBox("listcountry", Model.CountryListModel, new { @style = "width:200px;height:100px;" })
    <br /><br />
    <input type="submit" value="Submit" />
    <br />
    <div>
        Selected Country :@ViewBag.CountryName</div>
}

Now we will run the page to see the output. 


Now select the item and press submit. As we run our break point will hit. in this we will get the selected item value.


Now press F5 to view complete 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