加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Result(ActionResult、JsonResult、JavaScriptResult等)

发布时间:2020-12-16 18:44:21 所属栏目:百科 来源:网络整理
导读:一丶ActionResult 应用于Action方法前面的类型,它是Action的返回值,代表Action的执行结果。 public ActionResult Index(){ return View();} ?二丶JsonResult 返回Json字符串,但ajax接受到后,会自动转换成Json对象进行使用。 public ActionResult Val1() {

一丶ActionResult

应用于Action方法前面的类型,它是Action的返回值,代表Action的执行结果。

public ActionResult Index()
{
     return View();
}

?二丶JsonResult

返回Json字符串,但ajax接受到后,会自动转换成Json对象进行使用。

     public ActionResult Val1()
        {   //生成3个随机数
            Random rd = new Random();
            var a = rd.Next(1,1000);
            var b = rd.Next(10,1000);
            var c = rd.Next(100,1000);
            //申明一个匿名类
            var msg = new
            {
                a1 = a,a2 = b,a3 = c,};
            return Json(msg);
        } 

?

三丶JavaScriptResult

通过后台返给前端js代码。

1      public ActionResult GetJs()
2         {
3             return JavaScript("alert(‘我是js代码,调用的是JavaScriptResult‘)");
4         }
5         public ActionResult GetJs2()
6         {
7             return Content("alert(‘我是js代码,调用的是ContentResult,并自定义的返回类型为js‘)","application/x-javascript");
8         }

四丶FileResult

应用于下载文件

 public FileResult DownLoadExcel(StudentInfo studentInfo,Student_Photo student_Photo,string dateStart,string dateEnd)
        {
            List<StuInfoAndImg> stuInfoAndImgList = GetStu_List(studentInfo,student_Photo,dateStart,dateEnd);
            string pathFileName = string.Empty;
            ExportStudentExcel(stuInfoAndImgList,out pathFileName);
            string fileName = DateTime.Now.ToString("yyyyMMddHHmmssffffff") + ".xlsx";

            return File(pathFileName,"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",fileName);
        } 

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读