zoukankan      html  css  js  c++  java
  • MVC JsonResult的使用

    首先看一下我在控制器Controller中的代码

            public JsonResult SearchUnitByID(string ID)
            {
                Flow_StateUnitDefine unit = new Flow_StateUnitDefine();
                unit = FlowAccessor.GetUnitByID(ID);
                return this.Json(unit);
            }

    前端JS请求和返回的代码

        function OrgFocusedRowChanged(s, e) {
            var ID = s.GetRowKey(s.GetFocusedRowIndex());
            if (ID != null) {
                $.post("../Flow/SearchUnitByID?MenuID=" + getQueryStringRegExp('MenuID'),
                       { ID: s.GetRowKey(s.GetFocusedRowIndex()) },
                       function (data) {
                           txtState.SetValue(data.StateName);
                           txtUnit.SetValue(data.UnitName);
                           if (data.DataShowMeta == "null") {
                               memoData.SetValue("");
                           }
                           else {
                               memoData.SetValue(data.DataShowMeta);
                           }
                           txtID.SetValue(data.ID);
                           if (data.Type == "1") {
                               cboType.SetSelectedIndex(0);
                           }
                           else if (data.Type == "2") {
                               cboType.SetSelectedIndex(1);
                           }
                           else {
                               cboType.SetSelectedIndex(-1);
                           }
                       });
            }
            else {
                txtState.SetValue("");
                txtUnit.SetValue("");
                memoData.SetValue("");
                cboType.SetSelectedIndex(-1);
            }
        }

    此时看一下前端Js中用的是Jquery中的post请求,那么在控制器中没什么特别的。

    如果前端请求用的是get,那么控制器中要有相应的调整,代码如下:

            public JsonResult SearchUnitByID(string ID)
            {
                Flow_StateUnitDefine unit = new Flow_StateUnitDefine();
                unit = FlowAccessor.GetUnitByID(ID);
                return this.Json(unit, JsonRequestBehavior.AllowGet);
            }

    可以看出在return时,this.Json多了一个参数,这就是要和前端是Get请求,还是Post请求相对应的。因为默认的情况下相当于Post请求,所以上面this.Json仅一个参数即可。

  • 相关阅读:
    scss rem 转换函数
    URL Scheme —— 唤端媒介
    extend 对象继承
    [转载]jdk1.8垃圾回收器
    [转载]java高分局之jstat命令使用
    一个用消息队列 的人,不知道为啥用 MQ,这就有点尴尬
    context-param 监听器 过滤器 servlet 拦截器的区别
    springSecurity源码分析——DelegatingFilterProxy类的作用
    Spring Security的核心拦截器
    CAS之TICKET
  • 原文地址:https://www.cnblogs.com/aehyok/p/2758280.html
Copyright © 2011-2022 走看看