日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > windows >内容正文

windows

[置顶]信息发布系统 Jquery+MVC架构开发(7) Controller层

發(fā)布時(shí)間:2024/8/22 windows 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [置顶]信息发布系统 Jquery+MVC架构开发(7) Controller层 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Controller 這一層首先要添加對(duì)WCF 的引用:

如下,輸入我們自己的wcf地址

http://localhost:8732/Design_Time_Addresses/InfoPub.BLLService/Service1/mex

?

為了解析嵌套結(jié)構(gòu)的類(lèi),我們加入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)

??????? {

??????????? // 從?¨?請(qǐng)?求¨?中D獲?取¨?提?¨¢交?的ì?參?數(shù)oy數(shù)oy據(jù)Y

??????????? var json = controllerContext.HttpContext.Request.Form[bindingContext.ModelName] as string;

?

??????????? // 提?¨¢交?參?數(shù)oy是o?對(duì)?象¨?

??????????? if (json.StartsWith("{") && json.EndsWith("}"))

??????????? {

??????????????? JavaScriptSerializer js = new JavaScriptSerializer();

??????????????? object obj = js.Deserialize<T>(json);

??????????????? return obj;

??????????? }

?

??????????? // 提?¨¢交?參?數(shù)oy是o?數(shù)oy組á¨|

??????????? if (json.StartsWith("[") && json.EndsWith("]"))

??????????? {

??????????????? JavaScriptSerializer js = new JavaScriptSerializer();

??????????????? List<T> obj = js.Deserialize<List<T>>(json);

?

??????????????? return obj;

??????????? }

?

?

??????????? return null;

??????? }

??? }

}

我們依次添加三個(gè)controller,Infocontroller,InfoTypeContrller,UserInfoContrller,如下:

注意我們添加空的controller即可,別的controller我們暫用不到,如下:

下面我們添加Controller方法,于InfoController為例說(shuō)明:

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);

??????? }

?

??? }

}

?

?

轉(zhuǎn)載于:https://www.cnblogs.com/wangzhanjianshe/archive/2011/07/24/2326404.html

總結(jié)

以上是生活随笔為你收集整理的[置顶]信息发布系统 Jquery+MVC架构开发(7) Controller层的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。