Answered by:
Dropdownlist Set selectedvalue to Dictionary

Question
-
User-1572886696 posted
Hi,
How can i set dropdownlist selectedvalue to Dictionary, ?
foreach(var field in Model.EnableFields)
{
@HTML.dropdownlistfor(m=>m.dictionary[field.Id], field.SelectItems)
}
SelectItems return enum Gender M, F
The page loading work correctly and Dropdownlist work great but when i post , the value in Dictionary is always 0.
My model is:
public Dictionary<string, decimal> Dictionary
Public class[] EnableFields
ThanksFriday, May 29, 2020 9:41 PM
Answers
-
User-1572886696 posted
Thank you very much, i found why is not working, i use TextFields instead of numericFields for List.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, May 31, 2020 1:35 PM
All replies
-
User-474980206 posted
The dropdown only posts one scaler value (the selected value) It does not bind to a dictionary, you should have a scaler value in the model to bind to. If set multiplied it’s an array or list of scalar values.
Saturday, May 30, 2020 3:17 AM -
User-1572886696 posted
Hi, thank you for your replay, you mean that i should create
List<modelExample> list and
Public class modelExample
{
Public string selectedValue;
Public decimal Id;
}
foreach(var field in model.EnableFields)
{
@Html.Dropdownlistfor(???Saturday, May 30, 2020 6:16 PM -
User-474980206 posted
no its:
pulic class MyViewModel { public IEnumerable<SelectListItem> ListValues {get; set;} // not posted back, built in controller public decimal ListId {get; set;} // list posted value converted to decimal - also used to set selected value } ... @Html.DropdownFor(m => m.ListId, Model.ListValues)
note: some people will pass the ListValues in the ViewBag, but I prefer the view model
Saturday, May 30, 2020 6:29 PM -
User-1572886696 posted
Hi,
the case is quite complicated, here is the case:
View:
@model Modelforeach (var field in Model.EnabledFields)
{
@switch (field.InputType)
{
......
case Type.List:
@Html.DropDownListFor(m => m.NumericFields[field.Id], field.SelectListItems) // SelectListItems => {Text="Men", Value ="M"}, {Text="Female", Value ="F"}
break;
.......
}Model:
public class Model
{
.....
public Dictionary<string, decimal> NumericFields { get; set; }
.....
public Model1[] EnabledFields { get; set; }
.....
}public class Model1
{
public string Name { get; internal set; }
public enum InputType { get; set; }
public string Id { get; internal set; }
public IEnumerable<SelectListItem> SelectListItems { get; set; }public Model1() { }
public Model1(string id)
{
Id = id;
}public Model1(string id, enum inputType)
: this(id)
{
Name = id;
InputType = inputType;
}
}Controller:
if (model.NumericFields != null) ======> Value always 0
{
foreach (var field in model.NumericFields)
{
if (standardFieldsIDs.Contains(field.Key)) standardFieldData.Add(field.Key, field.Value);
else if (extensionFieldsIds.Contains(field.Key))
{
if (field.Value <= 0 && listExtensionFieldTemplateIds.Contains(field.Key))
continue; // avoids not assigned ExtensionFieldTemplateList values.extensionFieldData.Add(field.Key, field.Value);
}
}
}Sunday, May 31, 2020 11:39 AM -
User-1572886696 posted
Thank you very much, i found why is not working, i use TextFields instead of numericFields for List.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, May 31, 2020 1:35 PM