locked
How to make a value to be as default selected item in a selectList RRS feed

  • Question

  • User-997500692 posted

    Hi, I'm implementing asp.net core 3.1. In my razor view I have a selectList like the following in my razor view:

    <label asp-for="LastStatus" class="control-label mylabel">status</label>
                   
                <select asp-for="LastStatus" class="form-control">
                  
                    <option value="1">Active</option>
                    <option value="0">inactive</option>
                   
                </select>

    I want the active item to be the default selected item. but after choosing the active and then create a record when the status is Active, it returns null and set null in the related column. I appreciate if anyone suggests me a solution for this.

    Monday, February 8, 2021 9:47 AM

All replies

  • User1312693872 posted

    Hi,minamrm

    but after choosing the active and then create a record when the status is Active, it returns null and set null in the related column.

    You can check my demo, use selected can set the default selected item.

    @model MyModel
    <form asp-action="getstatus" method="post">
    <label asp-for="LastStatus" class="control-label mylabel">status</label>
    
    <select asp-for="LastStatus" class="form-control">
    
        <option value="1" selected>Active</option>
        <option value="0">inactive</option>
    
    </select>
        <input type="submit" value="submit"/>
    </form>

    Controller to get the selected value

     public IActionResult getstatus(MyModel myModel)
            {
                ...
            }
    //Or just the LastStatus:
    public IActionResult getstatus(string LastStatus)
            {
              ...
            }

    Result:

    Best Regards,

    Jerry Cai

    Tuesday, February 9, 2021 1:59 AM
  • User-1982062310 posted

    minamrm

    I want the active item to be the default selected item. but after choosing the active and then create a record when the status is Active, it returns null and set null in the related column. I appreciate if anyone suggests me a solution for this.

    You need to use "asp-for" attribute of the "<select>" tag helper for that, setting the value of that Model field in your GET controller method, as well as maintaining it in POST controller method if validation fails and you need to re-render it.

    See here for an example: https://docs.microsoft.com/en-us/aspnet/core/mvc/views/working-with-forms?view=aspnetcore-5.0#the-select-tag-helper

    // Controller
    public IActionResult Index()
    {
        var model = new CountryViewModel();
        model.Country = "CA";
        return View(model);
    }
    
    // View
    @model CountryViewModel
    
    <form asp-controller="Home" asp-action="Index" method="post">
        <select asp-for="Country" asp-items="Model.Countries"></select> 
        <br /><button type="submit">Register</button>
    </form>
    

    I also just wrote a quick article showing the above approach in more detail, and also outlining most common mistakes that devs make when using Select List and other controls in ASP.NET Core, and how to avoid these - check it out here:

    How To Set Default Option In Select List In Asp.NET Core Mvc

    Sunday, February 21, 2021 10:34 PM