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

  • 相关阅读:
    0x00000090 该内存不能read written
    AutoCAD系统变量:EDGEMODE
    AutoCAD.net: DoubleClick
    Access 类型转换函数
    无法更改文件夹的隐藏属性 解决方法!
    Windows防火墙无法启动解决办法
    AutoCAD.net: DrawOrderChange display order of the entities in the drawing
    C#调用C++编写的COM DLL
    编辑AutoCAD 2010中新出现的CUIx文件[转]
    hook钩子
  • 原文地址:https://www.cnblogs.com/aehyok/p/2758280.html
Copyright © 2011-2022 走看看