[置顶]信息发布系统 Jquery+MVC架构开发(7) Controller层
Controller 這一層首先要添加對WCF 的引用:
如下,輸入我們自己的wcf地址
http://localhost:8732/Design_Time_Addresses/InfoPub.BLLService/Service1/mex
?
為了解析嵌套結構的類,我們加入JsonBinder
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
?
namespace InfoPub.Controllers
{
??? public class JsonBinder<T> : IModelBinder
??? {
??????? public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
??????? {
??????????? // 從?¨?請?求¨?中D獲?取¨?提?¨¢交?的ì?參?數oy數oy據Y
??????????? var json = controllerContext.HttpContext.Request.Form[bindingContext.ModelName] as string;
?
??????????? // 提?¨¢交?參?數oy是o?對?象¨?
??????????? if (json.StartsWith("{") && json.EndsWith("}"))
??????????? {
??????????????? JavaScriptSerializer js = new JavaScriptSerializer();
??????????????? object obj = js.Deserialize<T>(json);
??????????????? return obj;
??????????? }
?
??????????? // 提?¨¢交?參?數oy是o?數oy組á¨|
??????????? if (json.StartsWith("[") && json.EndsWith("]"))
??????????? {
??????????????? JavaScriptSerializer js = new JavaScriptSerializer();
??????????????? List<T> obj = js.Deserialize<List<T>>(json);
?
??????????????? return obj;
??????????? }
?
?
??????????? return null;
??????? }
??? }
}
我們依次添加三個controller,Infocontroller,InfoTypeContrller,UserInfoContrller,如下:
注意我們添加空的controller即可,別的controller我們暫用不到,如下:
下面我們添加Controller方法,于InfoController為例說明:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using InfoPub.InfoPubService;
?
namespace InfoPub.Controllers
{
??? public class InfoController : Controller
??? {
??????? private InfoPubServiceClient infoPubService = new InfoPubServiceClient();
?
??????? public JsonResult GetInfoList([ModelBinder(typeof(JsonBinder<SearchInfo>))]SearchInfo searchInfo)
??????? {
??????????? InfoList infoList = new InfoList();
??????????? infoList = infoPubService.GetInfoList(searchInfo);
??????????? if (infoList.infoResult.Code != 0)
??????????? {
??????????????? return Json(new { Data = infoList, isSuccess = false, message = "GetInfoList fail ", errorCode = infoList.infoResult.Code }, "text/json", JsonRequestBehavior.AllowGet);
??????????? }
?
??????????? return Json(new { Data = infoList, isSuccess = true }, "text/json", JsonRequestBehavior.AllowGet);
??????? }
?
??????? public JsonResult GetInfoById(int infoId)
??????? {
??????????? InfoList infoList = new InfoList();
??????????? infoList = infoPubService.GetInfoById(infoId);
??????????? if (infoList.infoResult.Code != 0)
??????????? {
??????????????? return Json(new { Data = infoList, isSuccess = false, message = "GetInfoById fail ", errorCode = infoList.infoResult.Code }, "text/json", JsonRequestBehavior.AllowGet);
??????????? }
?
??????????? return Json(new { Data = infoList, isSuccess = true }, "text/json", JsonRequestBehavior.AllowGet);
??????? }
?
??????? public JsonResult AddInfo(Info info)
??????? {
??????????? InfoResult infoResult = new InfoResult();
??????????? infoResult = infoPubService.AddInfo(info);
??????????? if (infoResult.Code != 0)
??????????? {
??????????????? return Json(new { Data = infoResult, isSuccess = false, message = "AddInfo fail ", errorCode = infoResult.Code }, "text/json", JsonRequestBehavior.AllowGet);
??????????? }
?
??????????? return Json(new { Data = infoResult, isSuccess = true }, "text/json", JsonRequestBehavior.AllowGet);
??????? }
?
??????? public JsonResult UpdateInfo(Info info)
??????? {
??????????? InfoResult infoResult = new InfoResult();
??????????? infoResult = infoPubService.UpdateInfo(info);
??????????? if (infoResult.Code != 0)
??????????? {
??????????????? return Json(new { Data = infoResult, isSuccess = false, message = "UpdateInfo fail ", errorCode = infoResult.Code }, "text/json", JsonRequestBehavior.AllowGet);
??????????? }
?
??????????? return Json(new { Data = infoResult, isSuccess = true }, "text/json", JsonRequestBehavior.AllowGet);
??????? }
?
??????? public JsonResult DeleteInfo(int infoId)
??????? {
??????????? InfoResult infoResult = new InfoResult();
??????????? infoResult = infoPubService.DeleteInfo(infoId);
??????????? if (infoResult.Code != 0)
??????????? {
??????????????? return Json(new { Data = infoResult, isSuccess = false, message = "DeleteInfo fail ", errorCode = infoResult.Code }, "text/json", JsonRequestBehavior.AllowGet);
??????????? }
?
??????????? return Json(new { Data = infoResult, isSuccess = true }, "text/json", JsonRequestBehavior.AllowGet);
??????? }
?
??? }
}
?
?
轉載于:https://www.cnblogs.com/wangzhanjianshe/archive/2011/07/24/2326404.html
總結
以上是生活随笔為你收集整理的[置顶]信息发布系统 Jquery+MVC架构开发(7) Controller层的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 两点坐标间距离的算法以及验证【转】
- 下一篇: 逻辑地址、线性地址、物理地址和虚拟地址