I admit, I'm confused:
I'm trying to return a simple object that I've converted to JSON as follows:
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!"); }
});
My RouteTable entry looks like this:
routes.MapRoute(
"UpdateCoder",
"provider/UpdateCoder/{coderDTO}", // URL with parameters
new { controller = "Provider", action = "UpdateCoder", coderDTO = UrlParameter.Optional }
);
my Controler action looks like this:
[AcceptVerbs(HttpVerbs.Post)]
public string UpdateCoder( string coderDTO )
{
var rslt = "success";
//var coder = coderDTO.CoderDTOToCoder();
return rslt;
}
What I get in the UpdateCoder parameter ( string coderDTO ) is a null;
This is my fall-back position I'd rather send a JSON object (the coderJson) to the action but I get an error: "No parameterless constructor defined for this object." When I do that I'm changing the parameter type as follows:
[AcceptVerbs(HttpVerbs.Post)]
public string UpdateCoder( **CoderDTO coderDTO** )
{
var rslt = "success";
//var coder = coderDTO.CoderDTOToCoder();
return rslt;
}
along with: ValueProviderFactories.Factories.Add(new JsonValueProviderFactory()); in the Global.asax
the CoderDTO class looks like this:
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;
}
}
The coderJsonString looks like this:
{"Id":201,"CoderCode":"GP ","Sal":null,"LName":null,"FName":null,"MI":null,"IsContract":false,"IsInactive":false,"Comments":null,"CasesCoded":0,"isBeingEdited":false}
It's been a long day!! Thanks for any help, I'm having dinner!!