Use AJAX background query values to assign values to front-end controls under MVC

Source: Internet
Author: User
Tags date1

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.