Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I maintain drop down selections on a page with many dynamically named drop down lists in asp.net mvc 2?
    primarykey
    data
    text
    <p>I've read the links listed when I was creating this question. I think what I'm trying to do differs slightly. I am using ASP.NET MVC 2. </p> <p>Apologies for the incoming wall of text. The most brief way I can think to ask this question is immediately below. After that I go into more of the why and how of the question.</p> <p>[VERY QUICK POINT OF THE QUESTION] How do I return a dictionary as a JSON result from an action and what jquery/javascript could be used in the onLoad event of the drop down lists to search that JSON for a named element and if it is found, return the corresponding value and pre-select that value in the drop down list?</p> <p>[BACKGROUND] I am working on creating a project for work where the user select an option from 2 initial drop downs. The first drop contains a list of pdf file names. The second drop downs contains a list of database queries associated with an alias name (they do not see sql...they see the name of the corresponding stored procedure or view). After the user has made these 2 selections the page reloads with many more drop downs below in 2 columns.</p> <p>On the left column is a list of drop downs that all contain the same content: a list of the names of the text fields contained within the selected pdf (I am using CeTe software's Dynamic PDF to handle pdf interaction). On the right column will be a list of drop downs containing the field names of the selected query. Both columns will have an equal number of drop downs; the total number will be the number of fields on the pdf.</p> <p>[PROBLEM] I can load the page fine and populate the drop downs with their respective data. The problem comes after the submit button is clicked and the data is posted. Since the drop downs are created dynamically, I'm using a naming convention to link which selections belong to the pdf fields and which belong to the db fields. On the action side, I iterate over the FormCollection and create a dictionary where the key is the name of the element posted and the value is the value of the element that was submitted. I only add entries to the dictionary for elements that have a non-null, non-empty value.</p> <p>So far, I have all of that working. My issue, as you've probably guessed, is maintaining the selections when the page returns from the Action back to the view. I've got some really mutilated code that is close, but probably a world away from a good solution.</p> <p>I will post the code I have that makes sense below. Another idea I have, that I've no clue how to go about doing is to put a function in the onLoad event of the drop down lists and goes to a json action and sends posts the name of the drop down calling the action/script (whatever this becomes). If the name submitted exists in the dictionary where I'm storing the previously submitted names &amp; values of the drop down lists, the corresponding value that should be pre-selected is returned; otherwise nothing is returned and none of the drop down's values are pre-selected.</p> <p>I don't know much about JSON, but if I could return the previously posted name/value dictionary as a JSON result to the page, I could use jquery to parse the json and handle the preselection where needed on the drop down.</p> <p>See my current code below. It could be emulated by just creating stubs data for the SelectItemLists if you so cared. The current issue is that whatever the last name/value element is in the pre-selected dictionary is what is pre-selected for the rest of the list of drop downs. </p> <p>Ex:</p> <p>Selected values:</p> <pre><code>A FirstName B LastName C SSN &lt;no selection&gt; &lt;no selection&gt; &lt;no selection&gt; &lt;no selection&gt; &lt;no selection&gt; &lt;no selection&gt; &lt;no selection&gt; &lt;no selection&gt; </code></pre> <p>If the above is submitted, the result is the following:</p> <pre><code>A FirstName B LastName C SSN C SSN C SSN C SSN C SSN </code></pre> <p>Current code:</p> <pre><code> &lt;% using (Html.BeginForm()) { %&gt; &lt;% List&lt;SelectListItem&gt; mainPdfFieldNames = (List&lt;SelectListItem&gt;)ViewData["PdfFieldNames"]; List&lt;SelectListItem&gt; mainDbFieldNames = (List&lt;SelectListItem&gt;)ViewData["DbFieldNames"]; Dictionary&lt;String, String&gt; fieldValue = (Dictionary&lt;string, string&gt;)ViewData["selectedFieldValues"] ?? new Dictionary&lt;string, string&gt;(); %&gt; &lt;% for (int i = 0; i &lt; mainPdfFieldNames.Count; i++) { String pdfPreselectValue = string.Empty; String dbPreselectValue = string.Empty; fieldValue.TryGetValue("PdfFieldNames" + i.ToString(), out pdfPreselectValue); fieldValue.TryGetValue("DbFieldNames" + i.ToString(), out dbPreselectValue); IDictionary&lt;string, object&gt; pdfHtmlAttrib = new Dictionary&lt;string, object&gt;(); IDictionary&lt;string, object&gt; dbHtmlAttrib = new Dictionary&lt;string, object&gt;(); List&lt;SelectListItem&gt; pdfFieldNames = mainPdfFieldNames; List&lt;SelectListItem&gt; dbFieldNames = mainDbFieldNames; %&gt; &lt;%= Html.Encode(" PDF Selected value: " + pdfPreselectValue ) %&gt; &lt;%= Html.Encode(" DB Selected value: " + dbPreselectValue) %&gt; &lt;% if (!String.IsNullOrEmpty(pdfPreselectValue)) { pdfFieldNames.Find(p =&gt; p.Value.Equals(pdfPreselectValue)).Selected = true; pdfHtmlAttrib.Add("selected", null); } if (!String.IsNullOrEmpty(dbPreselectValue)) { dbFieldNames.Find(p =&gt; p.Value.Equals(dbPreselectValue)).Selected = true; dbHtmlAttrib.Add("selected", null); } %&gt; Select PDF Field: &lt;%=Html.DropDownList("PdfFieldNames" + i.ToString(), pdfFieldNames, "Select PDF Field", pdfHtmlAttrib)%&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Select Database Field: &lt;%=Html.DropDownList("DbFieldNames" + i.ToString(), dbFieldNames, "Select Database Field", dbHtmlAttrib)%&gt; &lt;br /&gt; &lt;% }%&gt; &lt;input type="submit" value="Submit" /&gt; &lt;% } %&gt; </code></pre> <p><strong>MAJOR EDIT 1</strong> OMITTED TO MAKE ROOM FOR SOLUTION</p> <p>I've decided to go a different route with this. I think this might be more simplified in the end. After the view posts to the action, I create a string variable from the Dictionary I was previously using. This string is put in ViewData and on the page is a hidden field. I have a javascript function that grabs this field and parses it. The nameValue field has the format of "fieldname,fieldvalue;fieldname,fieldvalue;fieldname,fieldvalue".</p> <p>I am posting some test code below. It's actually a full html page that I'm using to develop this process bit by bit. My only remaining issue is triggering code to load after the page has been loaded. This code will select all drop downs on the page, iterate over them, and pass the name of the drop down to the function I've created that handles pre-selecting a value on the drop down based on it's name and whether that name exists in the nameValue hidden field on the page.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload