Solved

asp.net-mvc4

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

830 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