0

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 })

enter image description here

 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);  
SumitS.
  • 65
  • 2
  • 12
  • 2
    Possible duplicate of [MVC5 - How to set "selectedValue" in DropDownListFor Html helper](https://stackoverflow.com/questions/41719293/mvc5-how-to-set-selectedvalue-in-dropdownlistfor-html-helper) – Tetsuya Yamamoto Jan 25 '19 at 07:09
  • The default value can be set when you create the `SelectList`, there's an overload that lets you choose the default value (you have to pass the ID of the default value) – nalka Jan 25 '19 at 07:21
  • there are dynamic IDs given to the dropdows @nalka – SumitS. Jan 25 '19 at 07:24
  • no i have multiselect @TetsuyaYamamoto – SumitS. Jan 25 '19 at 07:25
  • what do you mean dynamic IDs? – nalka Jan 25 '19 at 07:29
  • `MultiSelectList` has similar treatment as `SelectList` to set selected value - just pass desired value into `MultiSelectList` constructor's fourth parameter. – Tetsuya Yamamoto Jan 25 '19 at 07:31
  • i am giving this as 4rth parameter `string.IsNullOrEmpty(item.JobConstructionManagerId.ToString()) ? 0 : item.JobConstructionManagerId` – SumitS. Jan 25 '19 at 08:36

1 Answers1

1

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 })
TanvirArjel
  • 30,049
  • 14
  • 78
  • 114
  • still form collection does not takes dropdown values as 0 which are not selected ..it just ignores it and takes only selected dropdowns.. and its creating new option in select rather that assuming existing one(select one) as 0 value – SumitS. Jan 25 '19 at 12:06
  • Check my undated answer! Hope it will work now. Used `Selected = true` for the first item and it worked! – TanvirArjel Jan 25 '19 at 12:12