Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

asp.net-mvc4

Posted on 2014-12-16
4
Medium Priority
?
174 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 75

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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Integration Management Part 2

927 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