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#.Net, Enable 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.
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.
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.
Ones we click on Submit button we will be able to get the marketing id.
Download
0 comments:
Please let me know your view