[英]Sending data through controller, in mvc pattern using ajax
Using knockout I am trying to send data, from my UI to the controller. 我使用淘汰赛试图将数据从用户界面发送到控制器。 This is the javascript used to send my ajax request(PUT) 这是用于发送我的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"});
}
}
});
But when I debug it to see my controller, the string comming in is blank. 但是,当我调试它以查看我的控制器时,输入的字符串为空。 This is my controller 这是我的控制器
[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.");
}
}
Action will parse parameters from client by its name, so you need to pass parameter with name regRequirementJson
contains your json. Action将按名称解析来自客户端的参数,因此您需要传递名称regRequirementJson
包含您的json的参数。 So change this line 所以换这行
data: JSON.stringify(model)
to 至
data: { regRequirementJson: JSON.stringify(model) }
and remove contentType: jsonContentType
. 并删除contentType: jsonContentType
。
Or you can try another way. 或者您可以尝试另一种方式。 Since ASP.NET can deserialize json by itself you can keep your js code as is and update your controller to 由于ASP.NET本身可以反序列化json,因此您可以保持js代码不变并将控制器更新为
[HttpPut]
public async Task<JsonResult> UpdateRegistrationRequirementAsync(RegistrationRequirement regRequirementModel )
{
try
{
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.");
}
Since you are sending a "RegistrationRequirement" object then in your controller you can do it this way : 由于您要发送“ RegistrationRequirement”对象,因此可以在控制器中通过以下方式进行操作:
[HttpPut]
public async Task<JsonResult> UpdateRegistrationRequirementAsync(RegistrationRequirement registrationRequirement)
{
try
{
var response = await ServiceClient.L09PutRegistrationRequirementAsync(CurrentUser.PersonId, registrationRequirement);
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.");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.