我承认,我很困惑:
我正在尝试返回一个我已转换为JSON的简单对象,如下所示:
viewModel.updateCoder = function (coder) {
var coderJson = ko.toJSON(coder);
var coderJsonString = ko.utils.stringifyJson(coderJson);
$.ajax({
url: "provider/UpdateCoder",
type: 'POST',
dataType: 'text',
data: coderJsonString,
contentType: 'text/csv',
success: function () { alert("Updated!"); }
});
我的RouteTable条目如下所示:
routes.MapRoute(
"UpdateCoder",
"provider/UpdateCoder/{coderDTO}", // URL with parameters
new { controller = "Provider", action = "UpdateCoder", coderDTO = UrlParameter.Optional }
);
我的Controler动作如下所示:
[AcceptVerbs(HttpVerbs.Post)]
public string UpdateCoder( string coderDTO )
{
var rslt = "success";
//var coder = coderDTO.CoderDTOToCoder();
return rslt;
}
我在UpdateCoder参数(字符串coderDTO)中得到的是null;
这是我的后退位置我宁愿向操作发送一个JSON对象(coderJson),但是我收到一个错误:“没有为此对象定义无参数构造函数。”当我这样做时,我正在更改参数类型如下:
[AcceptVerbs(HttpVerbs.Post)]
public string UpdateCoder( **CoderDTO coderDTO** )
{
var rslt = "success";
//var coder = coderDTO.CoderDTOToCoder();
return rslt;
}
以及:ValueProviderFactories.Factories.Add(new JsonValueProviderFactory());在Global.asax
中CoderDTO类看起来像这样:
public class CoderDTO
{
public Int32 Id { get; set; }
public String CoderCode { get; set; }
public String Sal { get; set; }
public String LName { get; set; }
public String FName { get; set; }
public String MI { get; set; }
public String Facility { get; set; }
public String Title { get; set; }
public Boolean? IsContract { get; set; }
public Boolean? IsInactive { get; set; }
public Boolean? IsDeleted { get; set; }
public String Comments { get; set; }
public String AlternateId { get; set; }
public int CasesCoded { get; set; }
public CoderDTO(Coder coder)
{
Id = coder.Id;
CoderCode = coder.CoderCode;
Sal = coder.Sal;
LName = coder.LName;
FName = coder.FName;
MI = coder.MI;
Facility = coder.Facility;
Title = coder.Title;
if (coder.IsContract != null) IsContract = coder.IsContract;
if (coder.IsInactive != null) IsInactive = coder.IsInactive;
if (coder.IsDeleted != null) IsDeleted = coder.IsDeleted;
Comments = coder.Comments;
AlternateId = coder.AlternateId;
}
public Coder CoderDTOToCoder()
{
var coder = new Coder
{
Id = Id,
CoderCode = CoderCode,
Sal = Sal,
LName = LName,
FName = FName,
MI = MI,
Facility = Facility,
Title = Title
};
coder.IsContract = IsContract ?? false;
coder.IsInactive = IsInactive ?? false;
coder.IsDeleted = IsDeleted ?? false;
coder.Comments = Comments;
coder.AlternateId = AlternateId;
return coder;
}
}
coderJsonString看起来像这样:
{"Id":201,"CoderCode":"GP ","Sal":null,"LName":null,"FName":null,"MI":null,"IsContract":false,"IsInactive":false,"Comments":null,"CasesCoded":0,"isBeingEdited":false}
这是漫长的一天!谢谢你的帮助,我正在吃晚饭!!
答案 0 :(得分:1)
我找到了为什么我不能返回反序列化到我的CoderDTO对象的JSON的问题的答案:我的对象没有无参数的公共构造函数。我有一个Coder的构造函数参数,它填充了CoderDTO。我把它拆分成一个单独的方法,现在可行了。
感谢帖子
StackOverflow - ASP.NET MVC 3 JSONP: Does this work with JsonValueProviderFactory?
答案 1 :(得分:0)
我认为你最好的办法是弄清楚为什么你不能反序化到你的DTO。您至少需要为其添加一个默认构造函数。
public CoderDTO() { }
对于你传递字符串的当前情况,我认为你想把它称为:
viewModel.updateCoder = function (coder) {
var coderJson = ko.toJSON(coder);
var coderJsonString = ko.utils.stringifyJson({ coderDTO: coderJson });
$.ajax({
url: "provider/UpdateCoder",
type: 'POST',
dataType: 'text',
data: coderJsonString,
contentType: 'application/json; charset=utf-8',
success: function () { alert("Updated!"); }
});
所以,基本上你创建一个带有参数名称和值的对象并对其进行字符串化。在这种情况下,coderJson是双重编码的。
答案 2 :(得分:0)
尝试更改您的操作以使用JsonResult:
[AcceptVerbs(HttpVerbs.Post)]
public JsonResult UpdateCoder(CoderDTO coderDTO)
{
var rslt = "success";
//var coder = coderDTO.CoderDTOToCoder();
return Json(rslt);
}