调用Action时访问动态属性.... MVC

时间:2015-05-05 16:18:43

标签: json asp.net-mvc

我有一个返回JSON的Action方法,为简洁起见,我排除了代码。 :

public ActionResult SetMasterLocation(string masterValue)
{
    json = new JavaScriptSerializer().Serialize(masterLocation);
    return Json(json, JsonRequestBehavior.AllowGet);
}

我需要调用此方法并访问返回的JSON字符串:

 var jVendors = SetMasterLocation(masterValue);

当我运行它并检查输出时,我在一个名为Data:

的动态属性中看到了JSON字符串

enter image description here

enter image description here

但是如果我尝试访问这样的数据,应用程序将无法编译,因为编译器说无法解析符号'数据':

      var jVendors = SetMasterLocation(masterValue);
        var data = jVendors.Data;

如何在运行时访问Data属性?

1 个答案:

答案 0 :(得分:1)

返回SELECT Name, Number, (SELECT COUNT(1) FROM (SELECT DISTINCT Street FROM tableA b WHERE a.Name = b.Name AND a.Number = b.Number AND b.Type = 'VILLA' AND b.Value IN (5, 1, 9)) x) as NumberOfStreetsForVillas, (SELECT COUNT(1) FROM (SELECT DISTINCT Street FROM tableA b WHERE a.Name = b.Name AND a.Number = b.Number AND b.Type = 'SMALL' AND b.Value IN (1, 2, 3)) x) as NumberOfStreetsForSmallHouses, FROM dbo.tableA a GROUP BY Name, Number

JsonResult

然后,您可以访问结果的 return new JsonResult() { Data = someData, JsonRequestBehavior = JsonRequestBehavior.AllowGet }; 属性