currently have following jquery code. when clicks on #docs, send .selected rows controller , model object back. can see, print out .append(). there has better way this.
the problem rows being sent datatables object; haven't found way send razor code yet. possible?
i can't find way make razor handle ajax using datatables rows. i'd have actual object rather unreliable , ill-maintainable print-out of object.
is there better way go entirely?
$('#docs').click(function () { $.ajax({ type: "post", url: "/docs/send", data: { rows: json.stringify($(table.rows('.selected').data().toarray())), }, error: function (xhr, ajaxoptions, thrownerror) { alert(xhr.status); alert(thrownerror); } }) .success(function (data) { var result = $.parsejson(data); $("#documents").html(""); $.each(result, function (key, value) { $('#documents').append(key+" "+value+"<br>") }); }); }); //
controller code. send calls count. count returns number of doc objects have exact ids given.
public list<doc> count(string[] gre, upfile.applicationdbcontext db) { var list = new list<doc>(); var count = 0; if (gre.length > 0) { foreach (var item in db.docs) { count = 0; foreach (var device in item.devices) { (var = 0; < gre.length; i++) { if (gre[i]!=null && device.gre != null) { if (gre[i].contains(device.gre)) { count++; } } } } if (count == gre.length) { list.add(item); } } } return list; } public jsonresult send(string rows) { var greid = new string[0]; if (rows != null) { var count = 0; greid = new string[rows.split(new string[] { ":[" },stringsplitoptions.none).length-1]; foreach (var row in rows.split(':')) { var rowsplit = row.split(','); (var = 0; < rowsplit.length; i++) { if (rowsplit[i].contains("gre-")) { greid[count] = rowsplit[i]; count++; } } } } var serializer = new javascriptserializer(); var serializedresult = serializer.serialize(count(greid,db).toarray()); return json(serializedresult);
you're not going able make razor code handle ajax. razor code server side once view rendered in browser there no razor code on page longer. client side code such ajax function.
my suggestion either build api returns data row json , have ajax handle or create partial view ajax reloads.
Comments
Post a Comment