1

How can I set the selectedvalue property of a SelectList after it was instantiated without a selectedvalue.

Controller

var _walkInnVM = new WalkInnVM
{        
    ProspectHandledEmpList = new SelectList(_db.Employees
        .AsEnumerable()
        .Where(e => e.Id == Int32.Parse(Session["LoggedUserId"].ToString()))
        .ToList(), "Id", "Name")       
};

HTML

@Html.DropDownListFor(m => m.ProspectHandledEmpID,
    Model.ProspectHandledEmpList, "", 
    new { @class = "form-control select2 ", @id = "ddlProspectHandled" })
ksg
  • 3,927
  • 7
  • 51
  • 97
  • Do you want to set a default value for the select list? – Vini Nov 26 '15 at 07:04
  • 2
    Just set the value of `ProspectHandledEmpID` - that's how model binding works. And setting the `Selected` property is pointless anyway since it would be ignored. Note also you do not need the `.ToList()` - its just unnecessary overhead. –  Nov 26 '15 at 07:05
  • @StephenMuecke A small example will be helpful.. – ksg Nov 26 '15 at 07:09
  • @ViniVasundharan Yeah..That was what I was looking for.. – ksg Nov 26 '15 at 07:11
  • 1
    In the GET method, just set the value of `ProspectHandledEmpID` to match one of the `Employee.Id` values before you pass the model to the view and it will be selected - `_walkInnVM.ProspectHandledEmpID = #; return View(_walkInnVM);` –  Nov 26 '15 at 07:12
  • `ProspectHandledEmpList = new SelectList(_db.Employees .AsEnumerable() .Where(e => e.Id == Int32.Parse(Session["LoggedUserId"].ToString())) .ToList(), "Id", "Name", default value) `, is this what you were looking for? – Vini Nov 26 '15 at 07:14
  • @StephenMuecke : i have done that based on your suggestion for some question of mine, where i wanted to pass a selected value to the dropdownlist – Vini Nov 26 '15 at 07:16
  • @ViniVasundharan, Setting the `selectedValue` parameter is just ignored. Its the value of the property (`ProspectHandledEmpID`) which determines what is selected. –  Nov 26 '15 at 07:17
  • OK. I thought OP wanted to pass a value to the View where the selected value comes on top of the dropdown. As an eg: if i have an edit view i would pass the already existing value to the view so that the dropdown holds that particular value. Because i have asked this question and wanted to disable the dropdown with the selected value passed. I thought OP wanted the same. my bad :( – Vini Nov 26 '15 at 07:18
  • @ViniVasundharan, Yes, and the way to do that is by setting the value of the property that the dropdownlist is bound to (not by setting the last parameter of `SelectList` which is just ignored) –  Nov 26 '15 at 07:20
  • @StephenMuecke: OK. I understand. But `ViewBag.TechnicalCharacteristicID = new SelectList(db.TechnicalCharacteristic, "TechnicalCharacteristicID", "TCName",15)`, really works. i juz tried it now. I have no value bound for `TechnicalCharacteristic` in my model but only in the selectlist. Am i still missing your point? And I hope the OP found his answer. So lets be out of discussion . :) – Vini Nov 26 '15 at 07:25
  • @ViniVasundharan, If you bound that to a property with a value of (say) 10, then the option with `value="10"` will be selected, not the one with `value="15"` (its simply ignored when you binding to a model property) –  Nov 26 '15 at 07:28
  • @ViniVasundharan I think your answer was correct in the case of using `ViewBag`,where I am not using it.Sorry If im wrong.. – ksg Nov 26 '15 at 07:29
  • OK. Now i understand. You are telling about binding the value to the model when it is being passed back to the controller. – Vini Nov 26 '15 at 07:30
  • @ksg : please follow stephen's instructions :) Hope you found your answer.. – Vini Nov 26 '15 at 07:31
  • @ViniVasundharan yes STeve was right ;) – ksg Nov 26 '15 at 07:32
  • @StephenMuecke If possible kindly look into this [this question](http://stackoverflow.com/questions/33936941/foolproof-requiredif-condition-with-false-status-not-working) – ksg Nov 26 '15 at 11:49

1 Answers1

1

You need to set the value of ProspectHandledEmpID in the model before you pass it to the view

var _walkInnVM = new WalkInnVM
{
    ProspectHandledEmpID = someValue, // add this
    ProspectHandledEmpList = new SelectList(_db.Employees.AsEnumerable()
      .Where(e => e.Id == Int32.Parse(Session["LoggedUserId"].ToString())), "Id", "Name")       
};

If db.Employees contains items with Id values from 1 to 10 and you set the value ProspectHandledEmpID = 3, then the 3rd option will be selected when the view is first generated.

  • If possible kindly look into [this question](http://stackoverflow.com/questions/34081348/loading-remote-data-in-select2) – ksg Dec 04 '15 at 05:46