0

I have a view with a script (included below) that fires on a dropdownlist change, and the event handler should fill values for two text inputs. Values are received from controller.

The problem I'm having is, as soon as itemstatus list is populated, the controller returns back to view, and no values are passed back to AJAX function.

If I remove itemstatus list code and manually assign values as follows, it works:

var result = new { Quantity = "123", Price = "555"};

then it works.

I've also tried other ways to get data within controller but results were the same. Do you have any ideas what am I doing wrong and why controller returns back to view before "return Json"

<script type="text/javascript">

$(document).ready(function() {
    //Dropdownlist Selectedchange event
    $("#dropdown1").change(function ()
    {
        $("#txtQuantity").empty();
        $("#txtPrice").empty();
        $.ajax(
        {
            type: 'POST',
            url: '@Url.Action("GetValues")', 
            dataType: 'json',
            data: { id: $("#dropdown1").val() },
            success: function(data) 
            {
                $("#txtQuantity").val(data.Quantity);
                $("#txtPrice").val(data.Price);
            }
        })
    })
});

Controller:

 public JsonResult GetValues(int id)
    {

        List<Item> itemstatus =
           (from pd in db.Item

            where (pd.itemID == id) 
            select new Item()
            {
                itemPrice = pd.itemPrice,
                itemQuantity = pd.itemQuantity
            }).ToList();

         ...
         more code where i select what i need as a result i have two strings named itemquantity and itemprice
         ...

        var result = new { Quantity= itemquantity, Price = itemprice };
        return Json(result, JsonRequestBehavior.AllowGet);

}

clearlight
  • 12,255
  • 11
  • 57
  • 75
  • Sorry ! what does this mean ? **as soon as itemstatus list is populated controller returns back to view and no values are passed back to ajax function** ? – Shyju Jan 01 '17 at 03:30
  • Sorry, was not clear enough. So, as soon as this line is executed: List itemstatus = (from pd in db.Item where (pd.itemID == id) select new Item() { itemPrice = pd.itemPrice, itemQuantity = pd.itemQuantity }).ToList(); controller returns to view. – user3809390 Jan 01 '17 at 03:42
  • That is the part i don't understand **controller returns to view** ? How do you know that ? That method is executed via an ajax call from your current view. So you are still seeing the current view in your browser while that code is being executed. So what you mean by controller returns to view ? Which view ? You are already seeing a view. – Shyju Jan 01 '17 at 03:45
  • Also, it seems that function(data) does not start at all – user3809390 Jan 01 '17 at 03:46
  • 1
    Is your ajax call returning a 200 response ? Check your browser dev tools->network tab and see the ajax call's response – Shyju Jan 01 '17 at 03:47
  • Yes I have a view for adding new items to one of the tables in mssql database. What i need is to get available Quantity and current Price of item that is selected in dropdownlist. – user3809390 Jan 01 '17 at 03:48
  • Your code does that (looks fine to me,assuming your `GetValues` method is not crashing, but returning a 200 OK response. As i mentioned in my earlier comment, check the ajax call's response – Shyju Jan 01 '17 at 03:50
  • And yes I get a 200 response – user3809390 Jan 01 '17 at 03:51
  • so you are getting a 200 response, but your input fields are not updated with the Quantity and Price coming from server ? Are you getting the expected json response ? – Shyju Jan 01 '17 at 03:52
  • But no values are sent back to view and not all code in GetValues is executed. I've created breakpoints to see what is going on but no breakpoint is reached after that "List itemstatus..." part – user3809390 Jan 01 '17 at 03:56
  • You said you got a 200 response. You will get that when your `return Json` statement in your action method gets executed. I am confused about what your expected behavior ! – Shyju Jan 01 '17 at 03:57
  • My mistake. Error 500 is returned. Problem was same as this one: http://stackoverflow.com/questions/6919709/only-initializers-entity-members-and-entity-navigation-properties-are-supporte – user3809390 Jan 01 '17 at 04:07

1 Answers1

0

because you are sending a list, whose data values can be accessed using

success: function(data) 
        {
            $("#txtQuantity").val(data[0].Quantity);
            $("#txtPrice").val(data[0].Price);
        }