jquery - How can I call script inside script to refresh my Partial View in JavaScript with Ajax MVC asp.net -
how can call script inside ajax script refresh partial view or maybe there way refresh div=itemstable.
first script in section scripts display div=itemstable data webservice , database. second script buying item shop when user click buy. after user purchase item, script should run first script refresh data.
more information in code below.
my view: list
<div id="itemstable"> loading data... </div> @section scripts { <script> $(document).ready(function webservicescript() { if ("webservice" in window) { var ws = new webservice("ws://some_webservice"); ws.onmessage = function (evt) { var receivedmsg = evt.data; $.ajax({ url: '/store/listpartial', data: receivedmsg, type: 'post', datatype: "html", contenttype: 'application/json; charset=utf-8', error: function (err) { alert('error: ' + err.statustext); }, success: function (result) { $('#itemstable').html(result); }, [................] }; } }); // script above refreshing data in partialview (<div id="itemstable">) // when click "buy" button on partialview, run following script. function sendrequest(id, price) { if (confirm('are sure?')) { var request = { "id": id, "price": price }; $.ajax({ url: '/store/buy', // method changing quantity of items , return enum (result). that's why need refresh partialview data: json.stringify(request), type: 'post', contenttype: 'application/json; charset=utf-8', error: function (err) { alert('error: ' + err.statustext); }, success: function (result) { switch (result) { case "0": alert("success!"); $('#itemstable').loadscript(webservicescript()); // doesn't work. somehow want refresh itemstable // trying this: $('#itemstable').load('@url.action("listpartial", "store", null )'); // method listpartial in controller store expect data webservicescript, unfortunately it's wrong solution break; case "1": alert("fail!"); break; } }, async: true, processdata: false }); } }; </script> }
you can use ajax.beginform in main view:
@using (ajax.beginform("buy", "product", new ajaxoptions { onsuccess = "onsuccess", onfailure = "onfailure" }, new { id = "buyproductform" })) -- id id of form once hit "buy" button, ajax form call onsuccess call below script , here can call first script can update first div.
function onsuccess(result) { if (result) { alert("success"); } } hope helps.
Comments
Post a Comment