Skip to content
This repository

.getJason and dynamically created options in partial view MVC3 #733

Closed
allen-628 opened this Issue · 7 comments

3 participants

allen-628 Koen Punt Patrick Filler
allen-628

I am trying to dynamically create options items for a chosen in a partial view using MVC3. The items are called from the controller through .getJason(). The function is returning a series of options (in this country names and ids). I then use .html(items) to supply chosen with these options. I tried to use $('CountrySelect').chosen() as suggested in issue 363; however, the select element falls back to an html select element rather than the chosen eleement. Any suggestions as to how to retain the chosen element?

I am using JQuery 1.7.2

The list is returned through:
$.getJSON("controller/action", function (data) {
var items;
$.each(data, function (i, country) {
items += "" + country.Text + "";
});
$('.chzn-select').html(items); // 247 country elements retrieved here
});
And then I call--
$("CountrySelect').chosen();
Which results in an HTML select element.
If I use
$('.chzn-select').chosen();
then the chosen element is displayed but the options are empty.
Help would be greatly appreciated.
TIA Allen

Koen Punt
Collaborator

Is the <select> showing the options when chosen hasn't been applied?

allen-628

I am using Razor syntax and the following statement which does display the options in a select tag:
@Html.DropDownFor(ddl=>ddl.CountryId, new SelectList(Model.Countries, "Id", "Name",'Select your country")
Please note at this line $('.chzn-select').html(items); // 247 country elements retrieved here; there are 247 well formed option tags in var items with the correct value (countryId) and countryName between the opening and closing tag.
e.g.

<option value=1>Canada</option>
<option value=2>United States of America<\option>
... remaining 243 countries
Koen Punt
Collaborator

Please read what you just wrote and see if you can understand..
So please add some interpunction and make use of markdown for styling your code snippets. Then I'll try again.

allen-628

This is probably going to sound stupid How do I add interpunction and mark down? I am not familiar with this markup-markdown

Koen Punt
Collaborator

There is a link on the right top of the input field: http://github.github.com/github-flavored-markdown/

allen-628

I added the markdown to your question about the select tag. Please review that reply

Patrick Filler
Owner

Sorry @allen-628, but I'm having trouble understanding what this issue is about. Please re-open if you're still having the issue and include a fiddle that demonstrates exactly what the problem is.

Patrick Filler pfiller closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.