.net - Setting selected value in SelectList isn't working using DropDownListFor -
I'm creating a selectList for a drop
public class EditDeviceViewModel {public selectList DeviceTypeList {Received; Set; } Public Utility Type Type {Received; Set; } Public EditDeviceViewModel (device device, IEnumerable & lt; DeviceType & gt; DeviceTypes) {... DeviceTypeList = new selectList (DeviceTypes.OrderBy (t = & gt; t.Type), "id", "type", device. DeviceType.ID); SelectedDeviceType = device.DeviceType.ID; }} So in my opinion I am using HTML Assistant
@ Html.DropDownListFor (model = & gt; create a drop down; model .SelectedDeviceType, Model.DeviceTypeList, new {id = "devicetypelist"}) When the view is provided, the value selected by me when selecting selectList initialising is not. What am i doing wrong After the
dropdown will use the value of the list SelectedDeviceType property as the selected value What you can do is set the property value when you build the model public class EditDeviceViewModel {get the public selectList DeviceTypeList { Set; } Public Utility Type Type {Received; Set; } Public EditDeviceViewModel (device device, IEnumerable & lt; DeviceType & gt; DeviceTypes) {... DeviceTypeList = New SelectList (DeviceTypes.OrderBy (t = & gt; T. type), "id", "type"); SelectedDeviceType = device.DeviceType.ID; ...}}
Comments
Post a Comment