Tuesday, 1 February 2022

Asp.Net Core 6: Bind ListBox(ListBoxFor) By EntityFrameWork Using C#.Net | Sql Server

2/01/2022 - By Pranav Singh 0

 In this article i will show you how you how you can bind or populate data and access selected record of   listbox at controller end using c#.net in asp.net core 6 or mvc.


Other Related Articles:

Asp.Net Core 6: Bind DropdownList And Display In Table & Access Selected Value (Webgrid) Using C#.NetEnable Disable Control in jQuery | CheckBox Check/UnCheck Enable Disable Control Using jQuery, jQuery MVC: Move Listbox Items From One Listbox to Another With jQuery And Detect At Controller End, MVC ListBoxFor Bind And Retrieve Selected Value Using C#.Net.


Now for this article first we will create a new asp.net core 6 web application. After creating asp.net core 6 or mvc application we will make connection to the database by using entityframework by our sql server database. After this we will be able to get our all DB table object on context. 



Here check the department table object  we will bind this table object to the ListBox control. Here is the Department Table fields.

using System;

using System.Collections.Generic; 

#nullable disable 

namespace Project.data

{

    public partial class Department

    {

        public int Id { get; set; }

        public string DepartmentName { get; set; }

    }

}

  

Now we will create a model class and add the below code in it. 


using Microsoft.AspNetCore.Mvc.Rendering;

using System.Collections.Generic; 

namespace Project.Models

{

    public class DepartmentModel

    { 

        public string SelectedDepartment { get; set; }

        public List<SelectListItem> DepartmentList { get; set; }

    }

}


In above class i have taken a collection as SelectListitem and a string type variable. Here in list we will add all the department data and in string variable we will capture the selected department id. Now we will create HttpGet method at controller end and add the below code.


[HttpGet]

        public IActionResult Index()

        {

            //Department list

            DepartmentModel departmentModel = new DepartmentModel();

            departmentModel.DepartmentList = new List<SelectListItem>();

 

            TestDBContext testDBContext = new TestDBContext();

            //Get department list

            var depdata = testDBContext.Departments;

            foreach (var ditem in depdata)

            {

                departmentModel.DepartmentList.Add(new SelectListItem

                {

                    Text = ditem.DepartmentName,

                    Value = Convert.ToString(ditem.Id)

                });

            }

            return View(departmentModel);

        }


In above code i have prepared the object of model class and then fetch data from data baes and bind it to model class list. Now we will create view and add the below code.


@model Project.Models.DepartmentModel

@{

    ViewData["Title"] = "Bind ListBox  Page";

} 

@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { @enctype = "multipart/form-data" }))

{

    <div class="text-center">

        @Html.ListBoxFor(m => m.SelectedDepartment, new SelectList(Model.DepartmentList, "Value", "Text"),new {@class="form-control" })<br/>

        <input type="submit" value="Submit" /><br />

    </div>

}


Now we will prepare the HttpPost controller method. So that we will be able to access the ListBox Selected value at controller end.

Asp.Net Core 6: Bind ListBox(ListBoxFor) By EntityFrameWork Using C#.Net | Sql Server

In above list we can see the list of all department in the listbox control from database. Now select a department and click on submit. 


Asp.Net Core 6: Bind ListBox(ListBoxFor) By EntityFrameWork Using C#.Net | Sql Server


Ones we click on Submit button we will be able to get the marketing id. 


Asp.Net Core 6: Bind ListBox(ListBoxFor) By EntityFrameWork Using C#.Net | Sql Server

Download

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