【发布时间】:2019-06-21 20:38:48
【问题描述】:
我正在尝试使用淘汰赛将数据从我的 UI 发送到控制器。这是用于发送我的 ajax 请求(PUT)的 javascript
var model = new Object();
model.StudentID = "";
model.ActiveProgram = "";
model.ProgramDesc = self.programData();
model.Cohorts = self.associationData();
model.LoadIntent = self.loadIntentData();
model.Francophone = self.frenchData();
model.Gender = self.genderData();
$.ajax({
url: putStudentRegRequirementsUrl,
type: "PUT",
contentType: jsonContentType,
dataType: "json",
data: JSON.stringify(model),
//jsonData:model,
success: function (data) {
$('#notificationHost').notificationCenter('addNotification', { message: "Updated.", type: "info" });
},
error: function (jqXHR, textStatus, errorThrown) {
if (jqXHR.status != 0)
{
$('#notificationHost').notificationCenter('addNotification', { message: "Unable to update registration requirement.", type: "error"});
}
}
});
但是当我调试它以查看我的控制器时,进入的字符串是空白的。这是我的控制器
[HttpPut]
public async Task<JsonResult> UpdateRegistrationRequirementAsync(string regRequirementJson)
{
try
{
var regRequirementModel = JsonConvert.DeserializeObject<RegistrationRequirement>(regRequirementJson);
var response = await ServiceClient.L09PutRegistrationRequirementAsync(CurrentUser.PersonId, regRequirementModel);
return Json(response);
}
catch( Exception ex)
{
Logger.Debug(ex, "Error updating Registration Requirement for user failed.");
Response.StatusCode = (int)HttpStatusCode.BadRequest;
return Json("Error updating Registration Requirement.");
}
}
【问题讨论】:
-
你的模型是一个对象,你的控制器需要一个字符串。
-
对不起,我在堆栈溢出时输入错误,已将模型作为 json 发送更正
-
尝试这样做:UpdateRegistrationRequirementAsync([FromBody]string regRequirementJson)
标签: javascript c# asp.net asp.net-mvc knockout.js