Beginner MVC, do a simple function today, is to enter Begindate and EndDate, from the background to assign value to another text box amount
The interface is very simple, the method is very simple, today using the jquery post method, first prepare the background code
PublicJsonresult Getamount (stringDate1,stringdate2) {DateTime begin=Datetime.minvalue; DateTime End=Datetime.minvalue; if(Datetime.tryparse (Date1, outBEGIN) &&Datetime.tryparse (Date2, outend)) { varList = db. Productdetails.where (d = d.date >= begin. Date && d.date <=end. Date). AsQueryable (); if(List! =NULL) { if(list. ToList (). Count >0) { varAmount = list. Sum (d =d.amount); returnJson (New string[] {amount. ToString ()}); } } } returnJson (New string[]{"0"}); }
Write the jquery code for the view again.
<script>$ (function () {$ ("#BeginDate"). blur (function () {Calamount ( This); }); $("#EndDate"). blur (function () {Calamount ( This); }); }); function Calamount (datet) {varD1 = $ ("#BeginDate"). Val (); varD2 = $ ("#EndDate"). Val (); $.post ("/payrecord/getamount", {date1:d1, date2:d2}, function (AA) {if(AA) {$ ("#Amount"). Val (aa[0]); } }, "JSON"); }</script>
OK, in this way, the realization of the effect, leaving the focus when the amount assignment, originally wanted to use the Change event, no effect, first use blur make use of it, and so find the reason to change.
Use AJAX background query values to assign values to front-end controls under MVC