zoukankan      html  css  js  c++  java
  • MVC中返回json数据的两种方式

    首先解释一下ActionResultIActionResult

    ActionResult一般用来返回ViewResult,PartialViewResult,JsonResult

    IActionResult一般用来返回预定义响应和错误,比如404

    MVC里面如果直接将数据返回到前端页面,我们常用的方式就是用return view();

    那么我不想直接用razor语法,毕竟razor这玩意儿实在是太难记了,还不如写ajax对接来得舒服不是

    那么我们可以这么做

    1.定义ActionResult,返回json,标记属性可以采用HttpPost,也可以是用HttpGet,按自己的需求来使用

    public ActionResult UpdateDownloadInJson(string deviceNames,string programNames)
            {
                string[] deviceName = deviceNames.Split(',');
                string[] programName = programNames.Split(',');
                List<DownloadViewModel> DownloadViewModelList = new List<DownloadViewModel>();
                foreach (string tempDeviceName in deviceName)
                {
                    var _deviceId=deviceInfoService.FindSingle<DeviceInfo>(r => r.DeviceName == tempDeviceName).Id;
                    foreach (string tempProgramName in programName)
                    {
                        int _programId = publishDetailService.Set<ProgramInfo>().Where(r => r.ProgramName == tempProgramName).FirstOrDefault().Id;
                        var progress= publishDetailService.Set<DeviceMaterial>().Where(r => r.DeviceId == _deviceId && r.ProgramId == _programId).FirstOrDefault().DownProgress;
                        DownloadViewModelList.Add(new DownloadViewModel
                        {
                            DeviceId= (int)_deviceId,
                            DeviceName = tempDeviceName,
                            ProgramName = tempProgramName,
                            DownloadProgress = (int)progress
                        });
                    }
                }
                return Json(new AjaxResult
                {
                    Result = DoResult.Success,
                    RetValue = DownloadViewModelList
                }, JsonRequestBehavior.AllowGet);
            }

    2.采用JsonResult,最主要拿来处理ajax请求

    [HttpPost]
            [HandlerAjaxOnly]
            public JsonResult CheckLogin(string username, string password, string code)
            {
                UserManage.LoginResult result = this.HttpContext.UserLogin(username, password, code);
                if (result == UserManage.LoginResult.Success)
                {
                    return Json(new AjaxResult { Result = DoResult.Success, DubugMessage = "登陆成功。" });
                }
                else
                {
                    return Json(new AjaxResult { Result = DoResult.Faild, DubugMessage = "登陆失败," + result.ToString() });
                }
            }

    具体的区别后续补充,用法基本就是这样。

    PS:

    补充部分:

    IActionResult的具体使用

    在具体定义时,可以看到,返回的结果是不一样的,这里可以返回预定义的响应,及一些错误

     

    public IActionResult Update(string id, Movie imovie)
    {
    var imovie = _movieService.Get(id);
    if (imovie== null)
    {
    return NotFound();
    }
    _movieService.Update(id, imovie);
    return NoContent();
    }

  • 相关阅读:
    Linux内核的整体框架
    Unix环境高级编程_文件和目录
    Unix环境高级编程_文件I/O
    u-boot启动的第二阶段
    linux基础之vi编辑器设置文件模板
    ARM linux开发之安装配置tftp
    STM32笔试题笔记
    linux基础之find命令常用用法
    ARM linux开发之根文件系统
    ARM linux开发之linux内核启动简介
  • 原文地址:https://www.cnblogs.com/hugeboke/p/14667476.html
Copyright © 2011-2022 走看看