您的位置:首页 > Web前端 > JQuery

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

2011-07-24 09:59 876 查看
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获?取¨?提¬¨¢交?的Ì?参?数ºy数ºy据Y

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

// 提¬¨¢交?参?数ºy是º?对?象¨®

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

{

JavaScriptSerializer js = new JavaScriptSerializer();

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

return obj;

}

// 提¬¨¢交?参?数ºy是º?数ºy组Á¨¦

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

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: