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

Closed
allen-628 opened this Issue Jul 22, 2012 · 7 comments

Comments

Projects
None yet
3 participants
@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

@koenpunt

This comment has been minimized.

Show comment Hide comment
@koenpunt

koenpunt Jul 24, 2012

Member

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

Member

koenpunt commented Jul 24, 2012

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

@allen-628

This comment has been minimized.

Show comment Hide comment
@allen-628

allen-628 Jul 28, 2012

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

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
@koenpunt

This comment has been minimized.

Show comment Hide comment
@koenpunt

koenpunt Jul 28, 2012

Member

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.

Member

koenpunt commented Jul 28, 2012

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 comment has been minimized.

Show comment Hide comment
@allen-628

allen-628 Jul 28, 2012

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

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

@koenpunt

This comment has been minimized.

Show comment Hide comment
@koenpunt

koenpunt Jul 28, 2012

Member

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

Member

koenpunt commented Jul 28, 2012

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

@allen-628

This comment has been minimized.

Show comment Hide comment
@allen-628

allen-628 Jul 28, 2012

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

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

@pfiller

This comment has been minimized.

Show comment Hide comment
@pfiller

pfiller Apr 24, 2013

Contributor

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.

Contributor

pfiller commented Apr 24, 2013

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.

@pfiller pfiller closed this Apr 24, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment