Reputation: 65
im using razor syntax i want to set value =0 if dropdown is not selected or drop down text is 'select one' and i have multiple dropdowns..and im getting this value by form collection
@Html.DropDownListFor(model => item.JobStaffId, (SelectList)newSelectStaffList,new { @class = "form-control js-select js-noFilter hidden DDStaff DD2 ",multiple="multiple", size = "2", Id = "JobStaffId" + t })
SelectList newSelectList = new SelectList((from s in Model.UserMasterList
.ToList()
select new
{
userId = s.userId,
userName = (s.userFirstName +' '+ s.userLastName)
}).Distinct()
,
"userId",
"userName",
string.IsNullOrEmpty(item.JobConstructionManagerId.ToString()) ? 0 : item.JobConstructionManagerId);
Upvotes: 0
Views: 1001
Reputation: 32099
You can do as follows:
@{
List<SelectListItem> selectListItems = newSelectStaffList.ToList();
selectListItems.Insert(0, (new SelectListItem { Text = "Select One", Value = "0", Selected = true }));
}
@Html.DropDownListFor(model => item.JobStaffId, selectListItems,new { @class = "form-control js-select js-noFilter hidden DDStaff DD2 ",multiple="multiple", size = "2", Id = "JobStaffId" + t })
Upvotes: 1