1

I've now tried like 20 different ways and Googled the issue but I'm still stuck. My list is always returning a null value. When I debug I can see the values are coming from the database, and everything seems fine.

But, when I use console.log to debug in the view, it is always null.

Here is my model:

public class GarageModel
{
    public int GarageId { get; set; }
    public int Values { get; set; }
}

C# Method:

public List <GarageModel> getRequestType(int garageId)
{
    List<GarageModel> newList = new List<GarageModel>();

    string sql = "SELECT GarageID, RequestProperty FROM GarageCrossRequestType 
                  WHERE GarageID = " + garageId;

    var cmd = new SqlCommand(sql, Connection);

    var dt = new DataTable();
    dt.Load(cmd.ExecuteReader());

    foreach (DataRow i in dt.Rows)
    {
        var model = new GarageModel();
        model.GarageId = Convert.ToInt32(i["GarageID"].ToString());
        model.Values = Convert.ToInt32(i["RequestProperty"].ToString());
        newList.Add(model);
    }

    return newList;
    // returns the values from database 
}

Javascript / jQuery:

 function editGarage(e) {
            e.preventDefault();
            var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
            var garageId = dataItem.GarageId;
            countryId = dataItem.CountryId;
            name = dataItem.Name;
            customerNumberOfEditingGarage = dataItem.CustomerNumber;
            var Values = dataItem.Values;

            $.ajax({
                   type: 'POST',
                   url: '@Url.Action("getRequestType", "Garage")?garageId=' + garageId,
                   dataType: 'JSON',
                   data: {
                       Values: Values, garageId: garageId
                   },
            });
            console.log(garageId, Values);
            // always return correct garageId but null in Values
            // or whatever value is set to Values in the model

What am I missing?

Like I've said I've tried different methods, I've changed types between get/post, I've changed the method to bool, int et cetera, but I'm lost and honestly, not especially good at JavaScript/jQuery either, so I'll appreciate any help I can get.

To specify, the result I would like is simply to see the Values I get from the method/database to be shown when I use console.log to debug in the browser.

  • 1
    Likely unrelated to your current issue, but https://stackoverflow.com/questions/14376473/what-are-good-ways-to-prevent-sql-injection may be of interest. And by "may be" I mean "you need to read it immediately". – mjwills Aug 18 '21 at 13:14
  • 1
    Your issue is that your `console.log(..)` is outside the ajax, so runs whether you run the ajax or not - your ajax request doesn't have a `success:` (or other callback) so your code as-is has no way to *receive* the response - so trying different get/post is irrelevant as you're ignoring the result when/if it *does* work. – freedomn-m Aug 18 '21 at 13:26
  • Does this answer your question? [How to return the response from an asynchronous call](https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – freedomn-m Aug 18 '21 at 13:35
  • @freedomn-m great point thank you! – BravaDejana Aug 18 '21 at 14:16
  • @mjwills you're not wrong! – BravaDejana Aug 18 '21 at 14:17

1 Answers1

2

Your getRequestType method needs only one param and values not a parameter so send only garageId and you need to fill values when your ajax success

$.ajax({
    type: 'POST',
    url: '@Url.Action("getRequestType", "Garage")',
    dataType: 'JSON',
    data: {
        garageId: garageId
    },
    success: function(data){
        values = data;
        console.log(garageId, data);
    }
});

And controller method needs to return JsonResult instead of list

public JsonResult /*List<GarageModel>*/ getRequestType ...


return Json(newList, JsonRequestBehavior.AllowGet);
Onur Gelmez
  • 1,044
  • 7
  • 9