System.web.mvc.JsonResult 类
JsonResult :代码简洁,返回的是与拼接一样的 json格式的数据
[HttpPost]
public JsonResult GetStoried(int ID)
{IList<BuildingNumber> buildingNumber = BuildingNumberBusiness.GetBuildingNumberByStreetalleyID( ID);
return Json(buildingNumber);
}
这种方式代码复杂,写起来花费时间相对也多些
[HttpPost] [HttpPost] public string GetStoried(int ID) { StringBuilder sbJson = new StringBuilder(); IList<BuildingNumber> buildingNumber = BuildingNumberBusiness.GetBuildingNumberByStreetalleyID( ID); sbJson.Append("["); sbJson.Append("{\"id\":\"" + "" + "\",\"text\":\"" + "--请选择--" + "\"},"); foreach (BuildingNumber list in buildingNumber) { sbJson.Append("{\"id\":\"" + list.BuildingNumberID.ToString() + "\",\"text\":\"" + list.BNName.Trim() + "\"},"); } sbJson.Remove(sbJson.Length - 1,1);//删除最后一个逗号 sbJson.Append("]"); return sbJson.ToString(); }
原文链接:https://www.f2er.com/json/290468.html