Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Default/Null Value with Select TagHelper

In asp.net mvc you can use:

@Html.DropDownListFor(model => model.Category, ViewBag.Category as IEnumerable<SelectListItem>, "-- SELECT --", new { @class = "form-control" }) 

Using asp.net 5, how do I include the default or null value (-- SELECT --) in a taghelper:

<select asp-for="Category" asp-items="@ViewBag.Category"  class="form-control"></select> 
like image 231
Reafidy Avatar asked Jan 18 '16 01:01

Reafidy


2 Answers

You can just insert an option item inside the select:

<select asp-for="Category" asp-items="@ViewBag.Category"  class="form-control">     <option disabled selected>--- SELECT ---</option> </select> 

The disabled keyword in the code above means that the "--- SELECT ---" row cannot be picked again once a choice has been selected in the list. If you want the user to be able to blank the selection again (i.e. if it's bound to a nullable field) then just omit disabled.

like image 141
Matt Avatar answered Sep 19 '22 08:09

Matt


If you want to store value null to database then use <option selected value="">Full Access</option>

like image 37
Muhammad Abrar Anwar Avatar answered Sep 21 '22 08:09

Muhammad Abrar Anwar