Why am I getting an error when trying to return the anonymous type to the client?

advertisements

In my action Method I try to return anonymous type:

public JsonResult GetAssociatedProperty(int id)
{
    try
    {
        var property = _repository.GetLayerProperty(id);

        return Json(new { Result = "OK",  new { property.Id, property.VectorLayerId, property.FieldName, property.FieldType, property.FieldValue, property.Required} }, JsonRequestBehavior.AllowGet);
    }
    catch (Exception ex)
    {

        throw;
    }
}

but I get this error:

Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.

On this row:

return Json(new { Result = "OK",  new { property.Id, property.VectorLayerId, property.FieldName, property.FieldType, property.FieldValue, property.Required} }, JsonRequestBehavior.AllowGet);

Any idea how can I fix the error and send anonymous type to the client?


As the error message tells you, you need a name for the second member of your anonymous type:

 return Json(new {
                Result = "OK",
                Prop = new { property.Id, property.VectorLayerId, property.FieldName, property.FieldType, property.FieldValue, property.Required} },
             JsonRequestBehavior.AllowGet);