Solved

asp.net-mvc4

Posted on 2014-12-16
4
168 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

752 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