asp.net - How can I dynamically add labels and checkboxes from a C# object or method to a fieldset using jQuery? -


i need populate fieldset on page pairs of labels/checkboxes differ user user. i've got hardcoded in html so:

old html:

    <fieldset id="checkboxesfieldset" data-role="controlgroup" data-type="horizontal" id="groupedsites">         <legend class="labeltext">select duckbilled platypi want include in backyard menagerie</legend>         <label for="duckbill1">1</label>         <input type="checkbox" name="duckbill1" id="duckbill1" />         <label for="duckbill2">2</label>         <input type="checkbox" name="duckbill2" id="duckbill2" /> 

. . .

i reckon new html should this:

    <fieldset id="checkboxesfieldset" data-role="controlgroup" data-type="horizontal" id="groupedsites">         <legend class="labeltext">select duckbilled platypi want include in backyard menagerie</legend>     </fieldset> 

...and c# , jquery in page (the jquery extreme pseudocode, can idea of want accomplish):

{//razor         ...     list<string> platypi = getplatypi(); } 

jquery:

$(function () {     $.each(string s in @platypi) {         $("#checkboxesfieldset").html.add.label(s);         $("#checkboxesfieldset").html.add.checkbox();     } } 

how should done?

if access string array:

   string[] sites = new string[] { "1", "2", "3", "7", "42" }; 

...you can way:

@foreach(var site in sites) {       @html.raw("<label for=" + site + ">" + site + " </label>");      @html.raw("<input type=\"checkbox\" name=" + site + " id=" + site + " />"); } 

if want use object in model populate, though:

@foreach(var site in model.listsites) {         @html.raw("<label for=" + @site.tostring() + ">" + @site.tostring() + " </label>");         @html.raw("<input type=\"checkbox\" name=" + @site.tostring() + " id=" + @site.tostring() + " />"); } 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -