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.
public IActionResult getstatus(MyModel myModel)
{
...
} //Or just the LastStatus: public IActionResult getstatus(string LastStatus) { ... }
Result:
Best Regards,
Jerry Cai
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
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.
// 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:
Member
1 Points
11 Posts
How to make a value to be as default selected item in a selectList
Feb 08, 2021 09:47 AM|minamrm|LINK
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.
Contributor
4923 Points
4198 Posts
Re: How to make a value to be as default selected item in a selectList
Feb 08, 2021 12:28 PM|DA924|LINK
https://www.geeksforgeeks.org/how-to-set-the-default-value-for-an-html-select-element/
Participant
970 Points
315 Posts
Re: How to make a value to be as default selected item in a selectList
Feb 09, 2021 01:59 AM|Jerry Cai|LINK
Hi,minamrm
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:
Result:
Best Regards,
Jerry Cai
Member
100 Points
31 Posts
Re: How to make a value to be as default selected item in a selectList
Feb 21, 2021 10:34 PM|artem_s|LINK
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
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