Database Operation On Javascript In Mvc
I am working on a mvc project. I have a button in my view which is calling a Javascript function to print the data. Code is as follows:
Solution 1:
You can call a ActionResult in controller from client side using Jquery ajax.
functionPrintDiv() {
var divToPrint = document.getElementById('printableArea');
var popupWin = window.open('', '_blank', 'width=300,height=300');
popupWin.document.open();
popupWin.document.write('<html><body onload="window.print()">' + printableArea.innerHTML + '</html>');
popupWin.document.close();
UpdateDb();
}
functionUpdateDb();
{
$.ajax({
type: 'POST',
url: "@Url.Content("/Contrller/ActionResult/")",
data : {
parameter1:value1,
parameter1: value2
},
dataType: datatype,
success:function(result)
{
//Any thing needed to be done on success
}
});
}
Now write your logic to update Db in your Actionresult
in Controller
.
Hope it helps.
Solution 2:
In general you never want to allow your View to access the database. That kind of access should be granted on an infrastructure level. I generally would not recommend this but at least do the database call on your Controller and then pass an IEnumerable to the View for the table.
Solution 3:
$.post("/Home/GetState/", { data : $("#printablearea").val() }, function (response) {
your code..
});
and in controller action
public actionresult GetState(string data)
{
datatable item = entity.datatable.contains(data);
entity.entry(item)..State = EntityState.Modified;
entity.savechanges():
}
Post a Comment for "Database Operation On Javascript In Mvc"