Search code examples
c#asp.net-coreasp.net-core-mvc

SelectList in ASP.NET Core 6


Selected Value not working in SelectList

entity "Material"

public class Material
{
    public int Id { get; set; }
    public string? Name { get; set; }
    public string? Unit { get; set; }
    public double? Count { get; set; }
    public double? Amount { get; set; }
    public double? Cost { get; set; }
}

data from db

part of code for controller

public async Task<IActionResult> Material{
    ...

    ViewData["Material"] = new SelectList(await Materials(), "Id", "Name",
                 sqlParameters[0].Value);
}

part of code for view

<select asp-for="Material" class="form-control" asp-items="ViewBag.Material"></select>

I tried to pass the selected value in 3 ways

  1. ViewData["Material"] = new SelectList(await Materials(), "Id", "Name", 21);

  2. ViewData["Material"] = new SelectList(await Materials(), "Id", "Name", "21");

  3. ViewData["Material"] = new SelectList(await Materials(), "Id", "Name", "Whey");

But I don't have any errors. In select list I see first item instead of second. List of items shows everything is fine and I have no problems here.

enter image description here

enter image description here


Solution

  • Selected Value not working in SelectList. But I don't have any errors. In select list I see first item instead of second. List of items shows everything is fine and I have no problems here.

    Actually, my first point at your <select asp-for="Material"> its a type of SelectList which expecting list of items, I am not quite sure if you have shared the correct model because here we need something type of selectList of List Of Object but your shared material is a single class.

    In addition, for keeping the dropdown item selected within SelectList we have overload items selected value as following:

    enter image description here

    In your scenario its Id more specifically, 21 (Whey) which you are trying to keep selected. But string wouldn't be the correct parameter which is a key but we need value.

    How I reproduce your Issue:

    Model:

     public class MaterialClass
        {
            public int Id { get; set; }
            public string? Name { get; set; }
    
        }
    

    Note: Just considering your Id and Name fields.

    View Model:

    public class MaterialViewModel
        {
            public List<SelectListItem> MaterialClassList { get; set; }
        }
    

    Note: As we need list of items so I made a view model of SelectListItem within <select asp-for="ListTypeObject"> thus, taking this view model. However, its not mandatory, you still can use Material class.

    Controller:

    public async Task<IActionResult> Material()
            {
    
    
                ViewData["Material"] = new SelectList(await Materials(), "Id", "Name", 21);
                return View();
                             
            }
    

    Materials() Method:

    As you haven't shared details of Materials method, therefore, assuming it returns a list of your MaterialClass objects. Hence, I have replicated as following:

    public async Task<List<MaterialClass>> Materials()
            {
                var materials = new List<MaterialClass>()
                     {
                      new MaterialClass {Name = "Milk", Id = 20},
                      new MaterialClass {Name = "Whey", Id= 21},
                      new MaterialClass {Name = "Jgv", Id = 22},
                     };
                return materials;
            }
    

    View:

    @model MyProject.ModelFolder.MaterialViewModel
    
    <select asp-for="MaterialClassList" class="form-control" asp-items="ViewBag.Material"></select>
    

    Debug:

    enter image description here

    Output:

    enter image description here

    enter image description here

    Note: If you noticed the output, I am getting the expected result. Just make sure about your sqlParameters[0].Value and try to debug and check if your selected value object has the correct parameters.