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.