Solved

asp.net-mvc4

Posted on 2014-12-16
4
159 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 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
which object should i use (efficiency) 5 45
New to SOAP and need some direction 10 40
Resolve Dependency Issues 4 41
IIS Authorization for Web Service 2 17
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.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

932 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

11 Experts available now in Live!

Get 1:1 Help Now