Reputation: 7289
I need to create an object in the javascript and pass it to handler
var EducationalInstitute = new Object();
EducationalInstitute.InstituteId = iInstituteId;
EducationalInstitute=Name;
Now i need to pass this in ajax
$.ajax({
url: "../Handlers/DeleteEducationalInstitutes.ashx",
dataType: "json",
responseType: "json",
cache: "false",
data: { EducationalInstitute: JSON.stringify(EducationalInstitute) },
success: DeleteEISuccess
});
The problem is I am not aware of how to get this in the handler as object!
var Institute = context.Request.QueryString["EducationalInstitute"];
EducationalInstitute educationalInstitute = (EducationalInstitute)new JavaScriptSerializer().DeserializeObject(Institute);
The value i am getting for Institute is {"InstituteId":"1"}
The class definition is
public class EducationalInstitute
{
[DataMember]
public int InstituteID { get; set; }
[DataMember]
public string InstituteName { get; set; }
[DataMember]
public string Zone { get; set; }
}
I am getting the error
{"Unable to cast object of type 'System.Collections.Generic.Dictionary`2[System.String,System.Object]' to type 'namespace.BusinessEntities.EducationalInstitute'."}
Upvotes: 0
Views: 1515
Reputation: 7289
This is how i did it
var Team = { EducationalInstitute: EducationalInstitute, Location: "Chennai", Mentor: Mentor, TeamDescription: TeamDescription, TeamId: 1, TeamLeader:"",TeamLogo: TeamLogo, TeamName: TeamName };
Ajax call
$.ajax({
url: "../Handlers/CreateTeam.ashx",
dataType: "json",
responseType: "json",
data: {Team:JSON.stringify(Team)},
success: createTeamSuccess,
error:createTeamError
});
C# Code
Team team=new Team();
var teamObject = context.Request.QueryString["Team"];
team = (Team)new JavaScriptSerializer().Deserialize(teamObject,typeof(Team));
Upvotes: 0
Reputation: 4918
I think it's the way you're creating the model in the javascript:
var model= {InstituteId: iInstituteId, InstituteName: iName};
where iInstituteId
& iName
are vars you've created in the client side code
$.ajax({
url: "../Handlers/EducationalInstitutes.ashx",
dataType: "json",
responseType: "json",
cache: "false",
data: {EducationalInstitute:JSON.stringify(model)},
Here's an example: http://geekswithblogs.net/pabothu/archive/2011/05/21/passing-a-complex-json-object-to-ashx-and-reading-it.aspx
Upvotes: 1