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

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 { getset; }
    }
}

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.
  

Theo: http://www.aspdotnet-pools.com/

Nhận xét

Đăng nhận xét

Bài đăng phổ biến từ blog này

Khôi phục phân vùng ổ cứng do ghost nhầm theo Hieuit.net

Cách sử dụng 2 phương thức [httppost] và [httpget] trong MVC theo https://dzungdt.wordpress.com

MVC định dạng tiền tệ vnđ - Format currency vnd in MVC