【发布时间】:2021-10-20 06:58:21
【问题描述】:
我现在已经尝试了 20 种不同的方法,并在 Google 上搜索了这个问题,但我仍然卡住了。我的列表总是返回一个空值。当我调试时,我可以看到值来自数据库,一切似乎都很好。
但是,当我在视图中使用console.log 调试时,它总是为空。
这是我的模型:
public class GarageModel
{
public int GarageId { get; set; }
public int Values { get; set; }
}
C#方法:
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
我错过了什么?
就像我说过的那样,我尝试了不同的方法,我在 get/post 之间更改了类型,我已经将方法更改为 bool、int 等,但老实说,我迷路了,不是特别擅长JavaScript/jQuery 也可以,因此我将不胜感激。
要指定,我想要的结果只是查看当我使用console.log 在浏览器中调试时从方法/数据库中获得的值。
【问题讨论】:
-
可能与您当前的问题无关,但可能对stackoverflow.com/questions/14376473/… 感兴趣。 “可能”是指“您需要立即阅读”。
-
您的问题是您的
console.log(..)在 ajax 之外,因此无论您是否运行 ajax 都会运行 - 您的 ajax 请求没有success:(或其他回调)所以您的代码原样无法接收响应 - 因此尝试不同的获取/发布是无关紧要的,因为当/如果它确实工作时你会忽略结果。 -
@freedomn-m 非常感谢!
-
@mjwills 你没看错!
标签: javascript c# jquery asp.net-mvc