• [置顶]信息发布系统 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获?取¨?提¬¨¢交?的Ì?参?数º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);

            }

     

        }

    }

     

     

  • 相关阅读:
    未能写入输出文件..”“拒绝访问。”的解决办法
    SecureCRT显示中文和语法高亮
    危险无处不在 Html标签带来的安全隐患(转载)
    具有负载均衡功能的MySQL服务器集群部署及实现
    Linux以及各大发行版介绍
    yum源 redis 设置
    freebsd+apache+mysql+php+phpmyadmin+zend+discuz傻瓜式教程
    VS2005无法切换到设计视图的解决方案
    IIS不能下载EXE的解决方法
    JQuery优秀插件征集
  • 原文地址:https://www.cnblogs.com/encounter/p/2188755.html
Copyright © 2020-2023  润新知