Reputation: 309
Here is my AJAX request:
var data = modalDom.find("form").serializeObject();
data["returnJson"] = true;
$.ajax({
type: "POST",
url: "/companies/edit/",
data: data,
dataType: "JSON",
success: function (result) {
modalDom.modal('hide');
var dropdown = $("#create-modal #CompanyID");
var currentSelected = dropdown.find("option:selected");
if (currentSelected.length) {
currentSelected.removeAttr("selected");
}
var newOption = '<option value="' + result.company.CompanyID + '">' + result.company.Name + '</option>';
dropdown.append(newOption);
dropdown.val(result.company.CompanyID);
}
});
And here is the controller it is hitting:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Company company) {
if (CurrentUser.IsCompany(User)) {
return AjaxJsonResult.AuthFailResult();
}
if (!ModelState.IsValid) {
return AjaxJsonResult.FailResult(ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage).ToList());
}
db.Company_CompanyType.RemoveRange(db.Company_CompanyType.Where(c => c.CompanyID == company.CompanyID));
if (Request.Params["CompanyTypes[]"] != null) {
var companyTypes = (from item in Request.Params["CompanyTypes[]"].Split(new[] {','})
select db.CompanyTypes.FirstOrDefault(c => c.Name == item)
into ct
where ct != null
select new Company_CompanyType() {
CompanyID = company.CompanyID, CompanyTypeID = ct.CompanyTypeID
}).ToList();
db.Company_CompanyType.AddRange(companyTypes);
}
if (company.CompanyID <= 0) {
db.Companies.Add(company);
}
else {
db.Entry(company).State = EntityState.Modified;
}
db.SaveChanges();
return AjaxJsonResult.SuccessResult("?companyID=" + company.CompanyID);
}
I am unable to access any properties of my JSON object through the result variable. When I log the data I am passing in the console, it is the correct data from the form. When log the result variable in the console I get this:
Object {Success: true, RedirectTo: "?companyID=9818", Messages: null}
What am I doing wrong?
EDIT: My goal is to have the result object return data submitted from the form. Example:
result.company.CompanyID = 1988
result.company.Name = "My Company Name"
Source code for AjaxJsonResult.SuccessResult() method:
public class AjaxJsonResult {
public bool Success { get; set; }
public string RedirectTo { get; set; }
public List<string> Messages { get; set; }
public static JsonResult SuccessResult(string redirectTo) {
var result = new AjaxJsonResult {
Success = true,
RedirectTo = redirectTo,
Messages = null
};
return ToJsonResult(result);
}
public static JsonResult ToJsonResult(AjaxJsonResult result, JsonRequestBehavior behavior = JsonRequestBehavior.AllowGet) {
return new JsonResult() {
Data = result,
ContentType = (string) null,
ContentEncoding = (Encoding) null,
JsonRequestBehavior = behavior
};
}
}
Upvotes: 0
Views: 312
Reputation: 4440
The json object available to javascript can only have the same properties as the object serialized by the controller. So if you return an AjaxJsonResult
, you will only have access to result.Success
, result.RedirectTo
and result.Messages
. If you need the company object to be serialized you have two options:
JsonResult
without success, redirect and messages information and just rely on http status codes. This would involve changing the action method and it wouldn't work if you are using the success, redirect and messages data in javascript.AjaxJsonResult
so it has an extra property to store content, and also pass this in the SuccessResult
method. It can be strongly typed to the Company class or it could be a plain object. Here's an example:
public static JsonResult SuccessResult(string redirectTo, object data)
{
var result = new AjaxJsonResult
{
Data = data,
Success = true,
RedirectTo = redirectTo,
Messages = null
};
return ToJsonResult(result);
}
Then you have to pass the object in the method like so:
return AjaxJsonResult.SuccessResult("?companyID=" + company.CompanyID, new {company});
You would then be able to access the object's properties in javascript, but you have to be mindful of using the appropriate case and to also add the new Data property. If you do this, you will be able to get the company ID by typing result.Data.company.CompanyID
Upvotes: 1