Posts

Showing posts with the label ListBox

Pass the Selected Values ​​of the ListBox from View to Controller in ASP.NET Core Using jQuery Ajax

Image
This article describes how to pass/send the selected ListBox value to the controller using jQuery Ajax. Let's see it through 4 steps. Step 1: Controller The following is a controller code. Microsoft.AspNetCore.Mvc.Rendering is included to use the SelectListItem class. The controller has two action methods, Index() and SampleViewModel() . In the code, a list of movie names is sent to the viewer to bind to the list box. The SampleViewModel() method retrieves the selected movie ID and text and sends it to the results view. HomeController.cs using Microsoft.AspNetCore.Mvc.Rendering; public class HomeController : Controller {    public IActionResult Index()     {         List<SelectListItem> MoviesList = new List<SelectListItem>         {             new SelectListItem { Value = "1", Text = "Zootopia" },             new SelectListItem { Value =...

Pass the Selected Values ​​of the ListBox to Controller in ASP.NET Core

Image
This blog explains how to send selected values of ListBox to ASP.NET Core Controller. Let's look at this with an example. The following is the SampleViewModel class. It has two properties, Movies, and SelectedMovies. The property called Movies will bind the list of movies in the ListBox and the selected movie property will send the selected value from the view to the controller. SampleViewModel.cs using Microsoft.AspNetCore.Mvc.Rendering; public class SampleViewModel {    public List<SelectListItem> Movies { get; set; }    public string[] SelectedMovies { get; set; } } The following is a controller code. Microsoft.AspNetCore.Mvc.Rendering namespace is included to use SelectListItem. The list of movies is assigned to the MoviesList variable. That MoviesList is assigned to the Movies property in the SampleViewModel. Finally, SampleViewModel is sent to view. In the Result action method, the selected list is retrieved from the view and sent to th...