locked
How to set default value in select html RRS feed

  • Question

  • User-81839486 posted

    I have form

    <form asp-page-handler="numberOfRows" method="POST" asp-area="" asp-page="/PagesWithTables/ShowProduktyTable" >
    <button type="submit" class="btn btn-default">zmień</button>
    <select asp-for="InputShowOnPage.ShowOnPage"
    asp-items="Model.Items"></select>
    </form>

    also 

    public string ShowOnPage { get; set; }

    public IEnumerable<SelectListItem> Items { get; } = new List<SelectListItem>
    {
    new SelectListItem{Value= "10", Text="10"},
    new SelectListItem{Value= "25", Text= "25"},
    new SelectListItem{Value= "50", Text="50"},
    new SelectListItem{Value= "100", Text="100"},
    };

    how can I create default value in select to 50?

    Wednesday, August 19, 2020 4:17 PM

All replies

  • User475983607 posted

    Set the selected property to true.

    public IEnumerable<SelectListItem> Items { get; } = new List<SelectListItem>
    {
        new SelectListItem{Value= "10", Text="10"},
        new SelectListItem{Value= "25", Text= "25"},
        new SelectListItem{Value= "50", Text="50", Selected = true },
        new SelectListItem{Value= "100", Text="100"}
    };

    Wednesday, August 19, 2020 4:41 PM
  • User-474980206 posted

    just set the model value:   

       ShowOnPage = "50";

    Wednesday, August 19, 2020 5:11 PM