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仅一个参数即可。

  • 相关阅读:
    BZOJ2456: mode 众数卡空间
    BZOJ4128: Matrix 矩阵BSGS
    [SDOI2011]计算器 BSGS
    前台中文搜索到后台乱码
    批量删除实现js+springmvc
    基于Jquery+Ajax+Json实现分页显示
    分页条的制作
    input text中不能显示空格后的内容
    mysql存入中文乱码问题
    WEBROOT根目录 <%=request.getContextPath()%>
  • 原文地址:https://www.cnblogs.com/aehyok/p/2758280.html
Copyright © 2011-2022 走看看