I have a complex solution where I need to change values of DDLs from the controller.
Although I can get the inital values set - changes made by a post do not change the DDL selections.
My Code:
public class HomeController : Controller { public class ConfigEntryLine { public long ItemID { get; set; } public string ItemText { get; set; } public ConfigEntryLine() { } public ConfigEntryLine(long pItemIDVal, string pItemText) { ItemID = pItemIDVal; ItemText = pItemText; } } public class ConfigElement { public long ElementID { get; set; } public long SelectedID { get; set; } public List<ConfigEntryLine> Lines { get; set; } public ConfigElement() { Lines = new List<ConfigEntryLine>(); } public static ConfigElement CreateElement(long pInitID) { ConfigElement ceRet = new ConfigElement(); ceRet.ElementID = pInitID; if(pInitID == 1) { for(int nX = 1; nX < 5; nX++) { ceRet.Lines.Add(new ConfigEntryLine(nX, $"Outer Val: {nX}")); } ceRet.SelectedID = 2; } else if(pInitID == 2) { for(int nX = 11; nX < 15; nX++) { ceRet.Lines.Add(new ConfigEntryLine(nX, $"First Val: {nX}")); } ceRet.SelectedID = 12; } else { for(int nX = 21; nX < 25; nX++) { ceRet.Lines.Add(new ConfigEntryLine(nX, $"Second Val: {nX}")); } ceRet.SelectedID = 22; } return (ceRet); } } public class ProductConfigurationEntry { public string ActionName { get; set; } public string InfoMessage { get; set; } public ConfigElement OuterElement { get; set; } public List<ConfigElement> Elements { get; set; } public ProductConfigurationEntry() { Elements = new List<ConfigElement>(); } public static ProductConfigurationEntry CreateEntry() { ProductConfigurationEntry pceRet = new ProductConfigurationEntry(); pceRet.InfoMessage = "Clear"; pceRet.OuterElement = ConfigElement.CreateElement(1); pceRet.Elements.Add(ConfigElement.CreateElement(2)); pceRet.Elements.Add(ConfigElement.CreateElement(3)); return (pceRet); } } [HttpGet] public ActionResult Index() { return View(ProductConfigurationEntry.CreateEntry()); } [HttpPost] public ActionResult Index(ProductConfigurationEntry pEntry) { ProductConfigurationEntry pceRet = ProductConfigurationEntry.CreateEntry(); if(pEntry.ActionName == "ONE") { //resend outer made selection - keep first selected on client - change second from default to first pceRet.InfoMessage = $"{pEntry.OuterElement.SelectedID}, {pEntry.Elements[0].SelectedID}, {pEntry.Elements[1].SelectedID} resend outer made selection -keep first selected on client - change second from default to first"; pceRet.OuterElement.SelectedID = pEntry.OuterElement.SelectedID; pceRet.Elements[0].SelectedID = pEntry.Elements[0].SelectedID; pceRet.Elements[1].SelectedID = 21; } if(pEntry.ActionName == "TWO") { //set outer to first - set second to last - keep third default pceRet.InfoMessage = $"{pEntry.OuterElement.SelectedID}, {pEntry.Elements[0].SelectedID}, {pEntry.Elements[1].SelectedID} set outer to first - set second to first - keep third default"; pceRet.OuterElement.SelectedID = 1; pceRet.Elements[0].SelectedID = 11; } return View(pceRet); }My View:
@model WebApplication4.Controllers.HomeController.ProductConfigurationEntry@{ ViewBag.Title = "Home Page";}<h2>@Model.InfoMessage</h2>@Html.ActionLink("Abbrechen", "Index", null, new { @class = "btn btn-success" })@using(Html.BeginForm()) { @Html.AntiForgeryToken() <div class="form-horizontal"> <div class="form-group"> @Html.HiddenFor(model => model.ActionName) </div> <div class="form-group"> @(Html.Kendo().DropDownListFor(m => m.OuterElement.SelectedID) .DataTextField("ItemText") .DataValueField("ItemID") .AutoBind(true) .HtmlAttributes(new { style = "width:280px;font-size:small" }) .ValuePrimitive(true) .BindTo(Model.OuterElement.Lines) ) @Model.OuterElement.SelectedID </div> <div class="form-group"> <table class="table table-striped table-hover"> @{int nCnt = Model.Elements.Count;} @for(int nX = 0; nX < nCnt; nX++) { <tr> <td class="last-col"> @Html.EditorFor(model => model.Elements[nX], new { htmlAttributes = new { @class = "form-control" } }) @Model.Elements[nX].SelectedID </td> </tr> } </table> <div class="form-group" style="margin-right:0px"> <div class="pull-right"> <input id="SubHiddenButton" type="submit" style="display:none" value="Save" name="SubmitButton" /> <input id="SubButton" type="submit" value="Check 1" class="btn btn-success" name="SaveButton" onclick="return SaveData(this)" /> <input type="submit" value="Check 2" class="btn btn-info" name="CancelButton" onclick="return CheckSaveName(this)" /> </div> </div> </div> </div> }@section Scripts { @Scripts.Render("~/bundles/jqueryval") <script> function SaveData(theButton) { $("#ActionName").val("ONE"); $("#SubmitButton").click(); } function CheckSaveName(theButton) { $("#ActionName").val("TWO"); $("#SubmitButton").click(); } </script>}My Editor:
@model WebApplication4.Controllers.HomeController.ConfigElement@Html.HiddenFor(model => model.ElementID)@(Html.Kendo().DropDownListFor(m => m.SelectedID) .DataTextField("ItemText") .DataValueField("ItemID") .AutoBind(true) .HtmlAttributes(new { style = "width:280px;font-size:small" }) .ValuePrimitive(true) .BindTo(Model.Lines))When I Get the page in every DDL the second line is selected - so far so good.
After a post I get the new selected IDs - also correct.
But changing the values on the server an passing back the changed data doesn_t change the selection in the DDLs.
I display the "SelectedID" just to check - and YES this value reflects the changes - only the DDLs stay a their former selection.
Is this a bug - or am I missing something, or?????
