Solved

asp.net-mvc4

Posted on 2014-12-16
4
154 Views
Last Modified: 2014-12-18
Hi guys ,

I hope someone can help.

I have some webapp application mvc4 and I'm trying to pass some value from ajax to the controller and  get value back to the textbox.

please , see attachment how my app look like to get some idea.

Here is my jquery code:
 $(document).ready(function () {
            //##currency change vendor
            $('#Vendor').change(function () {
                var vend = $('#Vendor').val();
                Getcurrency(vend)

            });
        });

        function Getcurrency(vend) {
            $.ajax({
                url: "/Ajax/Getcurrency",
                data: { vend: vend },
                dataType: "json",
                type: "POST",
                error: function () {
                    alert("An error occurred.");
                },
                success: function (data) {
                    var vends = "";
                    $("#Exchrate").html(vend);

                }
            });
        }

Open in new window


My controller side code:

        //##getcurrency
        [HttpPost]
        public ActionResult Getcurrency(int vend)
        {
            Storecatalog st = new Storecatalog();
            rmsmasterdbtestEntities objNew = new rmsmasterdbtestEntities();
            var vendo = (from s in objNew.Suppliers
                         join c in objNew.Currencies
                         on s.CurrencyID equals c.ID
                         where s.ID == vend select new {c.ExchangeRate }).FirstOrDefault();

            st.Exchrate = Convert.ToDecimal(vendo.ExchangeRate);

            return Json(st);

Open in new window

0
Comment
Question by:Moti Mashiah
  • 2
4 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 40502977
Your controller action is returning JSON back, so your JQuery should be able to parse that. In your Getcurrency function, you can modify the success condition to inspect the data coming back from the controller.

e.g.

       function Getcurrency(vend) {
            $.ajax({
                url: "/Ajax/Getcurrency",
                data: { vend: vend },
                dataType: "json",
                type: "POST",
                error: function () {
                    alert("An error occurred.");
                },
                success: function (data) {
                    var vends = "";
                    $("#Exchrate").html(vend);
                    $('the textbox').text(data.Exchrate);
                }
            });
        }

Open in new window

0
 
LVL 1

Author Comment

by:Moti Mashiah
ID: 40503049
Thanks for the quick reply - here is what I did:

function Getcurrency(vend) {
            $.ajax({
                url: "/Ajax/Getcurrency",
                data: { vend: vend },
                dataType: "json",
                type: "POST",
                error: function () {
                    alert("An error occurred.");
                },
                success: function (data) {
                    var vends = "";
                    $("#Exchrate").html(vend);
                    $('Exchrate').text(data.Exchrate);

Open in new window

 //Exchrate is my textbox where I want to send the data back.



still i don't see the change rate when I'm changing the Vendor.
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 40507284
Assuming you are successfully returning the JSON data to the client, you probably just need to change this part;
success: function (data) {
    var vends = "";
    $("#Exchrate").html(vend);
    $('Exchrate').text(data.Exchrate);

Open in new window

To:
success: function (data) {
    $("#Exchrate").text(data.Exchrate);

Open in new window

0
 
LVL 1

Author Closing Comment

by:Moti Mashiah
ID: 40507313
Always the best solution by Carl Tawn.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now