Cascading dropdown boxes and MVC2http://forums.asp.net/t/1558922.aspx/1?Cascading+dropdown+boxes+and+MVC2Tue, 03 Aug 2010 09:13:04 -040015589223843389http://forums.asp.net/p/1558922/3843389.aspx/1?Cascading+dropdown+boxes+and+MVC2Cascading dropdown boxes and MVC2 <p>Hi,</p> <p>I am filling a dropdown box by using ViewState, now I need to be able to create more drowdown boxes and fill them based on what was sleected in prev box.</p> <p>I have a mehtod in the control that takes a parentId and returns a List&lt;Entity&gt;, when selecting a option in dropdownbox1 a call will be made with jquery with the parent id that returns the list, if there is a count &gt; 0 in the return collection a new dropdown will be created and populated. This should be reqursiv.</p> <p>I have looked at diffrent solutions on the net, there is some that just uses the &#36;.post and then there is those that uses &#36;.getJSON.</p> <p>How should I handle this? Is there any torturial to look at? And how do I get the final value back to the server when submitting?</p> <p>BestRegards<br> </p> 2010-05-17T14:23:38-04:003843697http://forums.asp.net/p/1558922/3843697.aspx/1?Re+Cascading+dropdown+boxes+and+MVC2Re: Cascading dropdown boxes and MVC2 <p>I had this problem just last week&nbsp;</p> <p><a href="/p/1557068/3835085.aspx#3835085">http://forums.asp.net/p/1557068/3835085.aspx#3835085</a></p> <p>Most, if not all the source is there&nbsp;</p> <p>Basically use jquery to call an Action that returns a JsonResult, then you can build the dropdowns from there</p> <p><br> </p> 2010-05-17T17:28:20-04:003843784http://forums.asp.net/p/1558922/3843784.aspx/1?Re+Cascading+dropdown+boxes+and+MVC2Re: Cascading dropdown boxes and MVC2 <p>Thanks!</p> <p>That works, but I need it to be Cascading in a dynamic way.</p> <p>If I select option 3 from the box1 then box2 should get all the items that have option 3 from box1 as its parent. If i select option 1 in box2 then I should get all the items with the parent option 1 from box1 in box3 ans so on. </p> <p>My code looks like this : </p> <p><pre class="prettyprint">$(function () { $('#ProductCategories').change(getModels); $('#ddlFirst').attr('disabled', true); $('#ddlSec').attr('disabled', true); }); function getModels() { var strOrganizationIDs = &quot;&quot;; $(&quot;#ProductCategories option:selected&quot;).each(function () { strOrganizationIDs &#43;= $(this)[0].value; }); var url = &quot;/AdCategory/GetCategoriesByParent/&quot; &#43; strOrganizationIDs; $.getJSON(url, null, function (data) { $(&quot;#ddlFirst&quot;).empty(); $.each(data, function (index, optionData) { $(&quot;#ddlFirst&quot;).append(&quot;&lt;option value='&quot; &#43; optionData.Value &#43; &quot;'&gt;&quot; &#43; optionData.Text &#43; &quot;&lt;/option&gt;&quot;); $(&quot;#ddlFirst&quot;).attr('disabled', false); }); }); };</pre></p> <p><br> This does work on the ProductCategories and the ddlFirst box but I don´t see how to make this dynamic. Also how do you submit the last selected value to the server side?</p> <p><br> </p> <p>BestRegards<br> </p> <p><br> </p> <p><br> </p> 2010-05-17T18:25:31-04:003844108http://forums.asp.net/p/1558922/3844108.aspx/1?Re+Cascading+dropdown+boxes+and+MVC2Re: Cascading dropdown boxes and MVC2 <p>I did kind of a brute force way to handle multiple cascading drop downs with the code, but i found this recently that we might want to use - its more elegant</p> <p><a href="http://jsbin.com/unope/edit">http://jsbin.com/unope/edit</a></p> 2010-05-17T21:48:28-04:003844701http://forums.asp.net/p/1558922/3844701.aspx/1?Re+Cascading+dropdown+boxes+and+MVC2Re: Cascading dropdown boxes and MVC2 <p>Okay, the preview of that code did not work, but I could try to implement it anyway.</p> <p>There is however just static elements but I supose I can hide and unhide and then have 8 dropdowns.</p> <p>U do how ever not understand how I get the value from the last box(what ever box this might be) back to the serverside?</p> <p>BestRegards<br> </p> <p><br> </p> 2010-05-18T06:20:44-04:003856056http://forums.asp.net/p/1558922/3856056.aspx/1?Re+Cascading+dropdown+boxes+and+MVC2Re: Cascading dropdown boxes and MVC2 <p>I am still having problem with this, pleas help.<br> </p> 2010-05-21T17:39:54-04:003856812http://forums.asp.net/p/1558922/3856812.aspx/1?Re+Cascading+dropdown+boxes+and+MVC2Re: Cascading dropdown boxes and MVC2 <p>Well what i did was on &quot;change&quot; of the dropdown, i got the selectId, then did the JSON call to my controller action and with its results cleared the &quot;dependant&quot; dropdown and repopulated it with the results</p> <p></p> <pre class="prettyprint">&lt;script type=&quot;text/javascript&quot;&gt; $(document).ready(function() { $(&quot;#ParentDropDown&quot;).change(function() { resetDropDown(&quot;#ChildDropDown&quot;); var selectedId = getSelectedId(&quot;#ParentDropDown&quot;); populateDropDown(&quot;#ChildDropDown&quot;, selectedId); }); }); function populateDropDown(dropDownId, selectedId) { if (selectedId != &quot;&quot; || selectedId == &quot;0&quot;) { $(dropDownId).removeAttr(&quot;disabled&quot;); var url = &quot;/MyController/Action/&quot; &#43; selectedId; $.getJSON(url, null, function(data) { $.each(data, function(index, optionData) { $(dropDownId).append(&quot;&lt;option value='&quot; &#43; optionData.Value &#43; &quot;'&gt;&quot; &#43; optionData.Text &#43; &quot;&lt;/option&gt;&quot;); }); }); } } function getSelectedId(dropdownId) { var selectedId = &quot;&quot;; $(dropdownId &#43; &quot; option:selected&quot;).each(function() { selectedId &#43;= $(this)[0].value; }); return selectedId; } function resetDropDown(dropDownId) { $(dropDownId).empty(); $(dropDownId).append(&quot;&lt;option value=''&gt;--- Select Something ---&lt;/option&gt;&quot;); $(dropDownId).attr(&quot;disabled&quot;, true); } &lt;/script&gt;</pre> <p><br> Hope this helps</p> <p></p> 2010-05-21T21:32:35-04:003860020http://forums.asp.net/p/1558922/3860020.aspx/1?Re+Cascading+dropdown+boxes+and+MVC2Re: Cascading dropdown boxes and MVC2 <p>Thanks! But how to you get the slected valu from the last dropdown to the servicen side? I am working with a typed view and this dropdown procedure will not be bound to the model(Or is it possible to do that).<br> </p> 2010-05-22T17:06:26-04:003870274http://forums.asp.net/p/1558922/3870274.aspx/1?Re+Cascading+dropdown+boxes+and+MVC2Re: Cascading dropdown boxes and MVC2 <p>Are you maby returning the form itself and then extracts the value from the proper dropdown?<br> </p> 2010-05-25T08:10:50-04:003872006http://forums.asp.net/p/1558922/3872006.aspx/1?Re+Cascading+dropdown+boxes+and+MVC2Re: Cascading dropdown boxes and MVC2 <p><u><b>Update:&nbsp; Forget the stuff below, too confusing.&nbsp; Slapped together a working sample for ya:&nbsp; <a href="http://downloads.krokonoster.com/krok_cascading_dropdown.zip">Download it here</a></b></u><br> </p> <p><br> </p> <p>Deleted: Too confusing (Not to mention this forum's functionality for posting lot of code with text in between is a pain.)<br> </p> 2010-05-25T19:16:16-04:003873056http://forums.asp.net/p/1558922/3873056.aspx/1?Re+Cascading+dropdown+boxes+and+MVC2Re: Cascading dropdown boxes and MVC2 <p>Try this:<br> </p> <p>http://weblogs.asp.net/rajbk/archive/2010/05/20/cascadingdropdown-jquery-plugin-for-asp-net-mvc.aspx</p> 2010-05-26T00:13:53-04:003878595http://forums.asp.net/p/1558922/3878595.aspx/1?Re+Cascading+dropdown+boxes+and+MVC2Re: Cascading dropdown boxes and MVC2 <p>Thanks for all your help.</p> <p>I have tried rajbk sugestion and that works fine to some point. I do however still have problems : </p> <ol> <li>How do I make this dynamic so it could add almost how many dropdown as the datamodel sugests?</li><li>How do I see to it that if a call returns 0 results then no more dropdowns should be shown and the choosen value should be the from the last dropdown.</li><li>How do I get the selected value from the last dropdown back to the serverside when hitting submit on the typed view?</li><li>What happens if the user are not using javascript? Is this somthing I should think about?<br> </li></ol> <p>//SnowJim<br> </p> <p><br> </p> 2010-05-28T06:54:39-04:003883259http://forums.asp.net/p/1558922/3883259.aspx/1?Re+Cascading+dropdown+boxes+and+MVC2Re: Cascading dropdown boxes and MVC2 <p>Hi,</p> <p>I have now solved the followin :</p> <p><br> </p> <ol> <li>Needs max 5 so I created 5 dropdowns hand hide them.</li><li>I have modified the script to hide the dropdowns until thay are loaded.<br> </li><li>When posting to the server I am checking whitch dropdown that are last set by usint Request.Form[&quot;category05&quot;] and then extracts the value.</li><li>No solution here, but I am thinking of require javascript to use the site. Is this a bad idé?</li></ol> <p>The problem that still exists is if the post goes to the service and the validation fails, then it will return with the view to the user again but the dropdowns will not be set. Im not sure how to fix this. I could extend the ModelView class with information about how each of the dropdowns is set, but Im not really sure how to do this the right way? When a selection are made there will go a call to the service to collect the collection for the new dropdown, this means that I can not just set the selected value direcly.</p> <p>Another way is to hide the dropdowns and show a label with the choosen category path and then a delete button that removes the label and then shows the first dropdown. This is not a nice solution becourse the enduser will have to set the whole path even if its just the last that should be changed.</p> <p>Any Idé How to solve this?<br> </p> 2010-05-30T11:31:15-04:003892790http://forums.asp.net/p/1558922/3892790.aspx/1?Re+Cascading+dropdown+boxes+and+MVC2Re: Cascading dropdown boxes and MVC2 <p>I really need help on this, I do not know how to maintain state beween posts? Any suggestion?</p> <p>BestRegards<br> </p> 2010-06-02T17:40:37-04:003892834http://forums.asp.net/p/1558922/3892834.aspx/1?Re+Cascading+dropdown+boxes+and+MVC2Re: Cascading dropdown boxes and MVC2 <p></p> <blockquote><span class="icon-blockquote"></span> <h4>SnowJim</h4> will not be bound to the model(Or is it possible to do that).</blockquote> <p></p> <p>Not only possible but it's the right way to go.</p> <p>I think that's what I have in that sample I posted for you.</p> <p><br> </p> 2010-06-02T17:52:24-04:003892877http://forums.asp.net/p/1558922/3892877.aspx/1?Re+Cascading+dropdown+boxes+and+MVC2Re: Cascading dropdown boxes and MVC2 <p>Yes, you are right it looks that way, I will investigate your sample some more. Thanks!<br> </p> 2010-06-02T18:13:00-04:003922891http://forums.asp.net/p/1558922/3922891.aspx/1?Re+Cascading+dropdown+boxes+and+MVC2Re: Cascading dropdown boxes and MVC2 <p>Hi,</p> <p>I have now investigated your sample and tried to change my own solution according to yours. But I do have a problem.</p> <p>My drop downs looks like this : </p> <p><pre class="prettyprint">&lt;%: Html.DropDownListFor(model =&gt; model.ModelViewAd.Category1, Model.ModelViewAd.Category1List, &quot;-- Välj kategori --&quot;)%&gt; &lt;%: Html.DropDownListFor(model =&gt; model.ModelViewAd.Category2, Model.ModelViewAd.Category2List, &quot;-- Välj kategori --&quot;)%&gt; &lt;%: Html.DropDownListFor(model =&gt; model.ModelViewAd.Category3, Model.ModelViewAd.Category3List, &quot;-- Välj kategori --&quot;)%&gt; &lt;%: Html.DropDownListFor(model =&gt; model.ModelViewAd.Category4, Model.ModelViewAd.Category4List, &quot;-- Välj kategori --&quot;)%&gt;</pre></p><p><br> In the rendered HTML page it looks like this :</p><p><pre class="prettyprint">&lt;select id="ModelViewAd_Category1" name="ModelViewAd.Category1"&gt;&lt;option value=""&gt;-- Välj kategori --&lt;/option&gt; &lt;option value="10"&gt;Fordon&lt;/option&gt; &lt;option value="15"&gt;För hemmet&lt;/option&gt; &lt;option value="17"&gt;Bostad&lt;/option&gt; &lt;/select&gt; &lt;select id="ModelViewAd_Category2" name="ModelViewAd.Category2"&gt;&lt;option value=""&gt;-- Välj kategori --&lt;/option&gt; &lt;/select&gt; &lt;select id="ModelViewAd_Category3" name="ModelViewAd.Category3"&gt;&lt;option value=""&gt;-- Välj kategori --&lt;/option&gt; &lt;/select&gt; &lt;select id="ModelViewAd_Category4" name="ModelViewAd.Category4"&gt;&lt;option value=""&gt;-- Välj kategori --&lt;/option&gt; &lt;/select&gt; </pre></p><p><br> I have changed the script to this : </p><p><pre class="prettyprint"> &lt;script type="text/javascript"&gt; &#36;(function () { &#36;("select#ModelViewAd_Category1").change(function () { var id = &#36;(this).val(); var urlAction = "/AdCategory/GetCategoriesByParent1/" + id; &#36;.getJSON(urlAction, { id: id }, function (data) { &#36;("#ModelViewAd_Category2").addItems(data.d); }); }); }); &lt;/script&gt;</pre></p><p><br> I get the following exception when running &#36;("#ModelViewAd_Category2").addItems(data.d);&nbsp; :</p>&lt;div role="listitem"&gt;X &#36;(&lt;/div&gt;<p> <span></span></p> &lt;div role="listitem"&gt;<img src="chrome://firebug/content/blank.gif" role="checkbox" aria-checked="aria-checked" title="Break on this error"><a> &#36;("#ModelViewAd_Category2").addItems(data.d); </a><br mce_bogus="1">&lt;/div&gt;<p>As you can see, I do not got a real exception message?</p><p>The response I am getting is </p><p><pre class="prettyprint">{"d":[{"Selected":false,"Text":"Lägenheter ","Value":"18"},{"Selected":false,"Text":"Fritidsboende ","Value":"19"}]}</pre></p> <p><br> Why am I getting this exception? And how do I fix it?</p> <p><br> </p> <p>Is there really no simpler way? I have invested as much time in this cascading dropdown solution as I have on the entire project and I have not yet a wokring sample. Its also seemse like I will not be able to do a entirely cascading dropdown where the dropdowns is created if needed, insted I will have do define the dropdowns before sending it to the client. I can live with this but it would be nicer with a real cascading dropdown solution.</p> <p><br> </p> <p>BestRegards<br> Jimmy<br> </p> 2010-06-12T19:04:04-04:003922903http://forums.asp.net/p/1558922/3922903.aspx/1?Re+Cascading+dropdown+boxes+and+MVC2Re: Cascading dropdown boxes and MVC2 <p>Did you include common.js or at least the two functions I got in there? (addItems and clearSelect)</p> <p>Not sure what your ViewModel looks like (<span><span>model.ModelViewAd.Category1.....??)</span></span></p> 2010-06-12T19:15:32-04:003922910http://forums.asp.net/p/1558922/3922910.aspx/1?Re+Cascading+dropdown+boxes+and+MVC2Re: Cascading dropdown boxes and MVC2 <p>Thanks for fast answer!! I hade missed the common file, when it was included it worked fine. I will ofcourse have to dig in to the code to be able to hide the dropdowns that are not in use but that should not be that hard I hope?</p> <p>Thanks for your solution, it seems simpler then the one I was first trying to implement.</p> <p>//SnowJim<br> </p> 2010-06-12T19:24:27-04:003922919http://forums.asp.net/p/1558922/3922919.aspx/1?Re+Cascading+dropdown+boxes+and+MVC2Re: Cascading dropdown boxes and MVC2 <p>Very simple to hide (jQuery's .hide() method should work)</p> <p>No worries, this solution works so well for me, I'm a bit concerned that I'm overlooking better ways.&nbsp; </p> <p>Pls do update me if you come across something that seems better.<br> </p> 2010-06-12T19:32:56-04:00