Default value for Html dropdown list - c#

I am using MVC4 and c#. I have a DropDownList which I set using ViewBag.
#Html.DropDownList("category_Parent", new SelectList(ViewBag.Category, "Id", "Name"), new { #class = "form-control" })
By default it always selects the first value but I want it to select a null value (a default value). How can I do this?

Related

Razor Html.DropDownListFor automatically set the selected value

I am new to Razor, I pass dropdown list as a viewbag and another values, when drop down list show in web it has selected values but i haven't set the any selected value, I think I use same name in viewbag value and dropdownlist value,it can be happen?
This is the viewbag,here i set the values and drop downlist:
ViewBag.GlobalInstance = globalInstance;
ViewBag.LegislativeCode = legislativeCode.Trim();
ViewBag.Legislatives = new SelectList(legislativeEntities, "LegislativeCode", "LegislativeName");
This is frontend code:
#if (ViewBag.GlobalInstance == true)
{
<div class="col-xs-5 hcm-form_controller mandatory gap" >
<label for="LegislativeCode" class="w-100" mi-resourcekey="atr-LegislativeCode" >Legislative Entity</label>
#Html.DropDownListFor(m => m.LegislativeCode, ViewBag.Legislatives as SelectList, "--- Select ---", new { #class = "calc-w-100" })
</div>
}
It seems that the page model has a value for LegislativeCode,
m => m.LegislativeCode
so when there is a same LegislativeCode in the ViewBag.Legislatives, this option will be selected.

Lookup Name Property from the ID in the database

I currently have the below snippet of code that works fine for Dropdown list. I would like to do the same thing but have it as a TextBoxFor field instead. I am utilizing bloodhound bootstrap plugin and replacing my dropdown list with the bootstrap plugin.
#Html.DropDownListFor(m => m.CaseMaster.InvoiceStatusId, new SelectList(Model.InvoiceStatus, "Id", "Name"), "Select Status", new { #class = "form-control col-sm" })

How to set default value for ASP.NET MVC DropDownList from ViewBag

In the controller Index I have the following:
ViewBag.Assignees = (await GetAllUsers()).Select(a =>
new SelectListItem
{
Text = a.DisplayName,
Value = a.Username,
Selected = a.DisplayName == "John Smith"
}).OrderBy(x => x.Text).ToList();
In the View, I have the following:
#Html.DropDownListFor(model => model.Assignee,
ViewBag.Assignees as List<SelectListItem>,
"Select Assignee",
new { id = "ddlAssignee", #class = "form-control"})
The dropdownlist populates as expected, however, the default (selected = true) value, which does exist, does not get set. Can someone advise what is wrong in the above?
UPDATE:
By Changing the SelectListItem.Value to a.DisplayName (same as SelectedListItem.Text) I achieved it. Still not sure what prevents the dropdownlist from displaying the item with Selected = true
If the model.Assignee comes with value, and if it is an int it will be defaulted to 0, it will override the SelectListItem selected value.
I suggest to set up the model.Assignee.
Here two ways that i use.
WAY 1
#Html.DropDownListFor(model => model.Assignee,
ViewBag.Assignees as List<SelectListItem>,
"Value", // property to be set as Value of dropdown item
"Text", // property to be used as text of dropdown item
"1"), // value that should be set selected of dropdown
new { id = "ddlAssignee", #class = "form-control"})
WAY 2
<select name="SelectName" value="1" class="w-100">
#foreach (var item in ViewBag.Collection) {
<option value="#item.Id">#item.Name</option>
}
</select>
I hope it work for you
#Html.DropDownListFor how to set default value
In your view set:
#Html.DropDownListFor(model => model.Assignee,
ViewBag.Assignees as List<SelectListItem>,
"Select Assignee",
new { id = "ddlAssignee", #class = "form-control", #value = "Default value"})
When you have list already defined.
Use This
#Html.DropDownList("CoverageDropDown", new SelectList(Model.youList, "Code", "Description",item.seletecItem), "Select")

Set Dropdown value to null when Page Loads MVC

I have created a DropDwonList in MVC like this
#Html.DropDownListFor(x => x.Item2.DeliveryDetail.PackageTypeId, new SelectList(lstPackageTypes, "PackageTypeId", "PackageTypeName"),new {#class = "cstm-input", #placeholder = "Package Type"})
It generates a DropDown but by default the first Item is selected when page loads. I want that the dropdown should be un-selected and the user should be allowed to select a value from it.
Thanx in Advance.
You can specify the default OptionLabel with overload method of DropDownListFor as below:
#Html.DropDownListFor(x => x.Item2.DeliveryDetail.PackageTypeId,
new SelectList(lstPackageTypes, "PackageTypeId", "PackageTypeName"),
"--Select Package Type--",
new {#class = "cstm-input", #placeholder = "Package Type"})

Having two values on SelectList() in asp.net

I'm trying to develop a website using asp.net mvc 4 & EF6 where I'm using dropdownlist to assign datas to my required textboxes. So far I've managed to assign data to two textboxes where one holds the value of the selected item of dropdownlist & one holds the text given for the selected item. But I need to add another value from the selected item. My codes are below,
Controller
ViewBag.Clients = new SelectList(db.ClientInfoes, "age", "fullname"); //This is given
ViewBag.Clients = new SelectList(db.ClientInfoes, "id", "age", "fullname"); //I want something like this
View
#Html.DropDownList("Clients", "Select...")
// Textboxes are populated automatically by using jQuery
#Html.TextBoxFor(a => a.Fullname, new { id = "clientName", #readonly = "readonly" })
#Html.TextBoxFor(a => a.Age, new { id = "clientAge", #readonly = "readonly" })
How can I assign more than one value in the SelectList? Any way I can do that would be fine. Need this help badly. Thanks.
It looks like you want to show name and age both in the dropdown text, in that case you need to project the result coming from database :
var ClientInfoes = db.ClientInfoes
.Select(x=>
new {
id = x.id,
name = x.fullname+","+x.age
});
ViewBag.Clients = new SelectList(ClientInfoes, "id", "name");

Categories

Resources