c# - MVC Two listboxes, Why does selected value moved to 2nd listbox flash and disapear? -



c# - MVC Two listboxes, Why does selected value moved to 2nd listbox flash and disapear? -

in so post, found out doing wrong jquery code transfer items between 2 mvc listboxes have. problem that, though code works, moves selected item first listbox 2nd split sec , disappears. don't know if client side problem or mvc problem?

the markup:

<head> <meta name="viewport" content="width=device-width" /> <title>index</title> <script src="~/scripts/jquery-2.1.1.js"></script> <script> $(function () { $("#add").click(function () { $("#listboxavail > option:selected").each(function () { $(this).remove().appendto("#listboxsel"); }); }); $("#remove").click(function () { $("#listboxsel > option:selected").each(function () { $(this).remove().appendto("#listboxavail"); }); }); }); </script> </head> <body> <div> @using (html.beginform()) { @html.listboxfor(m => m.selectedattributes, model.attributes, new {id="listboxavail", size = 5} ) <input type="submit" name="add" id="add" value="moveright" /> <input type="submit" name="remove" id="remove" value="moveleft" /> <input type="submit" name="remove-all" id="remove-all" value="remall" /> <input type="submit" name="select-all" id="select-all" value="selall" /> @html.listboxfor(m => m.selectedattributes2, model.selecteditems, new { id = "listboxsel", size = 5}) } </div> </body>

the mvc view model is:

public class optinviewmodel { public ienumerable<string> selectedattributes { get; set; } public ienumerable<string> selectedattributes2 { get; set; } public ienumerable<selectlistitem> attributes { get; set; } public ienumerable<selectlistitem> selecteditems { get; set; } }

and controller code is:

public actionresult index() { attributeentities db = new attributeentities(); list<selectlistitem> listselectlistitems = new list<selectlistitem>(); list<selectlistitem> listselitems = new list<selectlistitem>(); foreach (var attributes in db.harmonyattributes) { selectlistitem selectlist = new selectlistitem { text = attributes.attributename, value = attributes.atrributelabel, selected = false }; listselectlistitems.add(selectlist); } foreach (var sel in db.selectedharmonyattributes) { selectlistitem sellist = new selectlistitem { text = sel.customlabel, value = sel.harmonyattribute_id.tostring(), selected = false }; listselectlistitems.add(sellist); } optinviewmodel viewmodel = new optinviewmodel { attributes = listselectlistitems, selecteditems = listselitems }; homecoming view(viewmodel); }

your controls adding , removing <input type="submit" ..> submits form. alter them button elements

<button type="button" id="add">moveright"</button> <button type="button" id="remove">moveleft"</button> ....

c# jquery asp.net-mvc listbox

Comments

Popular posts from this blog

php - Edges appear in image after resizing -

ios8 - iOS custom keyboard - preserve state between appearances -

Delphi change the assembly code of a running process -