jquery - Retain multi-select list box items after ajax call -
i have view model renders n number of listboxes in partial view. each list box has unique id called partid. these listboxes allow users multi-select list items.
what trying before ajax request made, see if there listboxes nowadays , store respective listbox selected items indexes. if same litsbox rendered 1 time again retain previous selected items , pre-select on success function of ajax request.
these listboxes fetched via ajax
$.ajax({ type: 'post', url: servicelistpartsurl, cache: false, datatype: "html", data: { serviceentryid: $("#serviceentryid").val(), parts: parttextarea }, success: function (result) { $("#inputparts").html(result); } });
view model
using system.collections.generic; using runlog.domain.entities; namespace runlog.webui.models { public class serviceentrylistpartsviewmodel { public ienumerable<serviceentrypartdisplay> parts { get; set; } } }
partial view:
<tr> <td>@string.format("{0:mm/dd/yyyy hh:mm:ss tt}", model.partdescription) @html.hiddenfor(model => model.partdescription) </td> <td> @html.listboxfor(model => model.servicetypes, new multiselectlist(runlog.domain.lists.globallist.partsservicetypes(), "id", "name"), new { style = "width: 200px; height: 80px;", id = @model.partid, name = "listbox" }) </td> <td>@html.textareafor(model => model.comment, new { style = "width: 200px; height: 80px;" }) </td> </tr>
controller action
[httppost] //[outputcacheattribute(nostore=true,duration=0,varybyparam="*")] public viewresult listparts(string serviceentryid, string parts) { int id = convert.toint32(serviceentryid); serviceentrylistpartsviewmodel viewmodel = new serviceentrylistpartsviewmodel(); list<serviceentrypartdisplay> parts = new list<serviceentrypartdisplay>(); if (parts.length > 0) { var partsservicetyperesults = rec in db.serviceentrypart bring together ec in db.part on rec.partid equals ec.id (rec.serviceentryid.equals(id) && ec.active == true) orderby rec.serviceentryid select new serviceentrypartdisplay() { serviceentryid = rec.serviceentryid, partid = rec.partid, partdescription = ec.partdescription, servicetypeids = rec.servicetypeids, comment = rec.comment }; string[] splitdata = parts.split(new char[] { '|' }, stringsplitoptions.removeemptyentries); foreach (string split in splitdata) { serviceentrypartdisplay part = new serviceentrypartdisplay(); part.partdescription = split; part.partid = convert.toint32(split); part.partdescription = string.format("{0} ~ {1}", split, (from pp in db.part pp.id.equals(part.partid) select pp.partdescription).firstordefault()); var results = (from pp in partsservicetyperesults pp.partid.equals(part.partid) select new { pp.servicetypeids, pp.comment }).firstordefault(); if (results != null) { part.comment = results.comment; part.servicetypes = domain.lists.globallist.getpartsservicetypes(results.servicetypeids); } else { part.comment = ""; part.servicetypes = new list<domain.lists.partsservicetype>(); } parts.add(part); } } viewmodel.parts = parts; homecoming view(viewmodel); }
i think can simplify way you're doing it. why not pass controller collection of custom viewmodel
contains, say, servicetypeid
, selected parts id
. withi that, can generate partial view right items already selected. can trickery javascript clientside, i'd take advantage of tools have. post necessary info server , allow mvc thing help set view in right state.
jquery asp.net-mvc
No comments:
Post a Comment