ARTICLE AD BOX
I'm trying to bring data to a view using Razor, and it's not working only for a dropdown list.
I can get values for almost all fields on the view, except the dropdown is not being updated with the selected value. During debugging, I could confirm the value is present on the model for all fields (included the one to be used on dropdown).
Model:
public class MainViewModel { public string Prop1 { get; set; } public ComboList<Code> CodesToSelect { get; set; } public List<DetailMainViewModel> Details { get; set; } public MainViewModel() { FillCombo(); } private void FillCombo() { CodesToSelect = Helpers.CreateComboCodesToSelect(); } } public class DetailMainViewModel { public string DetailProp1 { get; set; } public int DetailProp2 { get; set; } public string DetailProp3 { get; set; } public Code? SelectedCode { get; set; } public DateTime? DetailDate { get; set; } }View:
@model MainViewModel <div class="col-sm-12"> <div class="col-sm-12"> <h5>Title</h5> </div> <div> @for (int i = 0; i < Model.Details.Count; i++) { <div class="row"> <div class="col-sm-1"> @Html.LabelFor(d => d.Details[i].DetailProp2, new { @class = "control-label" }) @Html.TextBoxFor(d => d.Details[i].DetailProp2, new { @class = "form-control", @readonly = "readonly" }) </div> <div class="col-sm-2"> @Html.LabelFor(d => d.Details[i].DetailProp3, new { @class = "control-label" }) @Html.TextBoxFor(d => d.Details[i].DetailProp3, new { @class = "form-control", @readonly = "readonly" }) </div> <div class="col-sm-3"> @Html.LabelFor(d => d.Details[i].SelectedCode, new { @class = "control-label" }) @Html.DropDownListFor(d => d.Details[i].SelectedCode, Model.CodesToSelect.ToSelectList(), new { @class = "form-control", @readonly = "readonly" }) </div> <div class="col-sm-2"> @Html.LabelFor(d => d.Details[i].DetailDate, new { @class = "control-label" }) @Html.TextBoxFor(d => d.Details[i].DetailDate, new { @class = "form-control", @readonly = "readonly" }) </div> </div> } </div> </div>As previously said, almost all the fields from DetailViewModel (DetailProp2, DetailProp3 and DetailData) are being populated and correctly displayed on screen. But SelectedCode is apparently ignored, with the dropdown displaying always the first item of the list.
That's my first post here ever, so please give me any hints to improve it.
