2 Replies - 404 Views - Last Post: 01 November 2013 - 07:48 AM

#1 Chario0z  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 23-April 12

Using JSon to print data (MVC, C#)

Posted 31 October 2013 - 09:52 AM

Hello. I'm trying to print out some information from two different classes/Controllers using JSon. However, I'm not getting the output 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.

Is This A Good Question/Topic? 0
  • +

Replies To: Using JSon to print data (MVC, C#)

#2 Chario0z  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 23-April 12

Re: Using JSon to print data (MVC, C#)

Posted 31 October 2013 - 01:55 PM

I tried this method as well:

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


Where productID is order[i].productID in my getBill function. However, when I call the method right after I set the productID to order[i].ProductID, It won't go ot the success branch, but error branch. The alert message gives me object[object] or something..
Was This Post Helpful? 0
  • +
  • -

#3 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3489
  • View blog
  • Posts: 10,057
  • Joined: 08-June 10

Re: Using JSon to print data (MVC, C#)

Posted 01 November 2013 - 07:48 AM

it doesn’t print the price because the price update is also asynchronous. i.e. if you call getPrice() it sends off an AJAX call, but the code (that called getPrice()) proceeds without waiting for the response, hence you’ll see the default price.

for the date it seems like your server returns a UNIX timestamp. either you change that on the server (returning a formatted date) or you create a Date object in JS and format the date from there.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1