0

Hello. I'm trying to print out some information using JSon. However, I'm not getting the numbers I expect. Can anyone see what I'm doing wrong?

Let me tell you about my code. Price is supposed to hold the price for a product. Variable out is my output.

The function getBill is supposed to print out the bill after an order. It prints some of the info, like Ordernumber and productID. However, it won't print out the date correctly and the price.

What it prints out: Price = 0
Date bought: /Date(1383221299497)/

ProductID and OrderID is fine.

<script type="text/javascript">
        var price = 0;
        var productID = 0;
        var out = "";

        function getBill() {
            $.getJSON("/User/GetOrder", { id: $("#idList").val() }, function(order) {
                for (var i in order) {
                    productID = order[i].productID + "";
                    getPrice();
                    out = " <br />Ordernumber: " + order[i].orderID + "<br />";
                    out += "ProductID: " + productID + "<br />";
                    out += "Date bought: " + order[i].dateBought + "<br />";
                    out += "Price: " + price;
                }
                $("#bill").html(out);
            });
        }

        function getPrice() {
            $.getJSON("/Shop/GetProduct", { id: $(productID).val() }, function(product) {
                for (var i in product) {
                    price = product[i].Price;
                }
            }); }

So, I'm using MVC here, so I have two controllers, ShopController and UserController. Both of them got methods, GetProduct and GetOrder. The #idList is a dropdownlist, I choose the ordernumber there to print out my bill.

The thing is, I'm not sure wether function getPrice is right or not. I'm not sure how to send the right parameters. Is it possible to send integer parameters?

My getPrice method looks like this:

public JsonResult GetProduct(String id)
        {
            int sId = Convert.ToInt32(id);
                List<Product> prod = db.Products.Where(o => o.ProductID == sId).ToList();
                JsonResult out = Json(prod, JsonRequestBehavior.AllowGet);
                return out;

        }

Why won't the price variabel get updated, when I call the method before printing out the bill? Why isn't dateBought output correct? How do I send integers as parameter instead of using string?

For date, I use this in my Order class.

public DateTime dateBought

Thanks in advance.

Edited by Shari_1: tyoos

1
Contributor
1
Reply
12
Views
4 Years
Discussion Span
Last Post by Shari_1
0

I changed my getPrice method to this:

function getPrice() {
            $.ajax (
                {
                    dataType: 'json',
                    url: "/Shop/GetProduct/",
                    data: { id: productID },
                    success: function(product) {
                        for (var i in product) {
                            price = product[i].Price;
                            alert(pris);
                        }
                    },
                    error: function(msg) {
                        alert(msg);
                    }
                }
            )
        }

However, it goes to the error branch, giving me object[object] error or something. Why won't it go to the success branch?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.