In above table we are having project detail which we are going to bind with listbox control. After this we will create a new asp.net core mvc application using c#.net. Now let's add a model class file and the below mention property in it.
public class EmployeeModel
{
public int[] MySselectedProjectId { get; set; }
public List<EmpProjectDetail> ProjectDetailList { get; set; }
}
public class EmpProjectDetail
{
public int Id { get; set; }
public string ProjectName { get; set; }
}
In above I have taken two class one for employee model and second for employee project model. In employee model class I have taken a property named as "MySselectedProjectId" of integer array type. This we will use to get the selected values of listbox control at controller end. Other one is for project list which we will bind with listbox control. Now let's add a controller and in this add an action method named as Index. [HttpGet]
public IActionResult Index()
{
EmployeeModel employeeModel = new EmployeeModel();
employeeModel.ProjectDetailList = new List<EmpProjectDetail>();
//Get data from DB
EmployeeContext employeeContext = new EmployeeContext();
var data = employeeContext.Projects.ToList();
foreach (var item in data)
{
employeeModel.ProjectDetailList.Add(new EmpProjectDetail { Id = item.Id, ProjectName = item.ProjectName });
}
return View(employeeModel);
}
In above code I have created the object of EmployeeModel and in next I have created the object of project list. In next line I have created the object of Employee DB context to get the project detail from the database. With the help of object of employee context, we will get the detail of project, and finally I have added the project detail in the model object. Finally, I have returned the object of employee model. Now we will create the view and the below code.@model EmployeeModel
@{
ViewData["Title"] = "Index";
}
@using (Html.BeginForm("", "", FormMethod.Post))
{
<label>My Projects:</label>
<br />
@Html.ListBoxFor(m => m.MySselectedProjectId, new SelectList(Model.ProjectDetailList, "Id", "ProjectName"), new { @class = "form-control" })
<br />
<input type="submit" value="Submit" />
<br />
@ViewBag.SelectedValue
}
In above code I have added the reference of the EmployeeModel class. After that I have defined the form tag. To display the listbox, I have used @Html.ListBoxFor control to display the listbox and label tag to display the text of the description. Here in ListBoxFor i have used array property defined in model class which help us to get the selected value at controller end. new SelectList is used to populate the value in listbox, In this project collection, value fields and text field value have been defined.
I have used input button of submit type to post the form and get the value at controller end. In last line I have used viewbag to display the user selected value. now let's create an HttpPost method at controller end.
[HttpPost]
public IActionResult Index(EmployeeModel employeeModel)
{
employeeModel.ProjectDetailList = new List<EmpProjectDetail>();
//Get data from DB
EmployeeContext employeeContext = new EmployeeContext();
var data = employeeContext.Projects.ToList();
foreach (var item in data)
{
employeeModel.ProjectDetailList.Add(new EmpProjectDetail { Id = item.Id, ProjectName = item.ProjectName });
}
string myselectedVal = string.Join(",", employeeModel.MySselectedProjectId);
ViewBag.SelectedValue = "Your selected Value: " + myselectedVal;
return View(employeeModel);
}
In above code I have passed the EmployeeModel as a parameter. In next list I have created the object of Project List. I have written code to get the data from the database. and the property MySselectedProjectId will provide us the selected value. Now we have done. Let's run the code and check the output.Now let's select "Project 2 and Project 4" and click on submit button. Ones we click break point will hit. Here is the controller code.