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#.
Some of my previous article are as follows: Error
: SQL Server Compact is not intended for ASP.NET development., Integrate
and Retrive HTML Textbox Editor Value In Asp.Net MVC Using C#.Net, Display
No of Characters Entered In TextArea Using jQuery, Multiple
File Upload With Asp.Net MVC C# and HTML5 | How to upload files to ASP.NET MVC
application, Display
Alert Message on Page Load in MVC Application Using Javasscript.
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.
0 comments:
Please let me know your view