asp.net mvc 4 - How to add an object to a listbox by clicking on checkbox? -
i have form list of checkboxes , listbox.
i need when check or uncheck checkbox, modifies list in listbox adding or removing object in list. how looks:

here code part of form:
<div class="row-fluid"> <div class="span3"> <label>fonction(s):</label> </div> <div class="span9"> @html.listboxfor(contact => contact.selectedfonctionids, model.listefonctionsselectlist, new { disabled = "disabled" }) </div> </div> <div class="row-fluid"> <div class="span5 offset3"> <div class="fonctions_container"> @foreach (extranetclient.models.classes.fonctioncontact fonction in viewbag.fonctions) { string coche = ""; if ((@model.listefonctions).any(c => c.idfonction == fonction.idfonction)) { coche = "checked"; } <input type="checkbox" @coche id="@fonction.libellefonction" onclick="javascript:ajouterfonction();" value="@fonction.idfonction" />@fonction.libellefonction <br /> } </div> </div> </div> does knows how that?
i thinking ajax thing it's typed view , datas associated model.
not sure if solution if put listbox partial view (strongly typed well), , ajouterfonction() javascript make ajax call action returns partialview containing updated version of listbox? use jquery replace previous listbox new one.
controller action:
public partialviewresult actionname(<whatever need here>) { // whatever logic may need: item clicked, should added model object, etc.... return partialview("listboxpartial", yourmodelobject); } partial view listboxpartial:
@model yourmodel @html.listboxfor(contact => contact.selectedfonctionids, model.listefonctionsselectlist, new {disabled = "disabled" }) in view, instead of
<div class="span9"> @html.listboxfor(contact => contact.selectedfonctionids, model.listefonctionsselectlist, new { disabled = "disabled" }) </div> you'd have:
<div class="span9"> @html.partial("listboxpartial", model) </div> your ajouterfonction(), after receiving new listbox, ajouterfonction() searches div class "span9" , replaces contents received data.
there may better way of solving this, first thing popped mind.
Comments
Post a Comment