MVC中几种常用ActionResult

1 篇文章 0 订阅
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;
using Newtonsoft.Json.Linq;
using System.IO;

namespace MvcApplication1.Controllers
{

    public class ViewModels
    {
        public Employee employee;
        public Company company;

        public ViewModels(Employee employee, Company company)
        {
            this.employee = employee;
            this.company = company;
        }
    }


    public class DemoController : Controller
    {   
        //
        // GET: /Demo/

        public ActionResult Index()
        {
            List<Employee> listEmployee = new List<Employee>();
            Random rnd = new Random();
            for (int i = 0; i < 30; i++)
            {
                Employee employee = new Employee();
                employee.Name = "Jack";
                employee.Num = "093432" + i;
                employee.Salray = rnd.Next(4000, 7000).ToString();
                listEmployee.Add(employee);
             
            }
          
          
            ViewData["time"] = DateTime.Now.ToString("yyyy-MM-dd");
            ViewBag.Tip = "Hello World";
            ViewData["DataList"] = listEmployee;

            return View(listEmployee);
        }

        public ActionResult About()
        {
            Employee employee = new Employee();
            employee.Name = "jack";
            employee.Num = "093431201";
            employee.Salray = "30000";
            Company company=new Company();
            company.Name="Water Way";
            company.Date="2012";
            var vm = new ViewModels(employee, company);
            return View(vm);
        }

        public ActionResult ContentResultDemo()
        {
            string str = "ContentResultDemo!";
            return Content(str);
        }

        public ActionResult EmptyResultDemo()
        {
            return new EmptyResult();
        }

        public ActionResult FlieContentResultDemo()
        {
            FileStream fs = new FileStream(Server.MapPath(@"/Images/0f000a-WuSD1wrfNOLDxt0.jpg"), FileMode.Open, FileAccess.Read);
            byte[] buffer = new byte[Convert.ToInt32(fs.Length)];
            fs.Read(buffer, 0, Convert.ToInt32(fs.Length));
            string contentType = "images/jpeg";
            return File(buffer, contentType);
        }

        public FilePathResult FilePathResultDemo(string id)
        {
            string path = Server.MapPath(@"/Images/" + id + ".jpg");
            string contentType = "image/jpeg";
            return File(path, contentType);
        }

        public ActionResult HttpUnauthorizedResultDemo()
        {
            return new HttpUnauthorizedResult();
        }

        public ActionResult HttpStatusCodeResultDemo()
        {
            return new HttpStatusCodeResult(500, "System Error");
        }

        public ActionResult HttpNotFoundResultDemo()
        {
            return new HttpNotFoundResult("not found action");
        }

        public ActionResult JavaScriptResultDemo()
        {
            return JavaScript(@"<script>alert('Test JavaScriptResultDemo!')</script>");
        }


      
        public ActionResult JsonResultDemo()
        {
            var tempOjb = new { controller = "HomeController", Action = "JsonResultDemo" };
            return Json(tempOjb,JsonRequestBehavior.AllowGet);//设置JsonRequestBehavior.AllowGet可以直接在浏览器里预览
           
        }


        public ActionResult RedirectResultDemo()
        {
            return Redirect(@"http://wwww.baidu.com");
        }


        public ActionResult RedirectToRouteResultDemo()
        {
            return RedirectToAction(@"About");
        }


        public PartialViewResult PartialViewResultDemo()
        {
            return PartialView();
        }


        


       

        [HttpPost]
        public JArray GetData()
        {
            JArray jarray = new JArray();
            Random rnd = new Random();
            for (int i = 0; i < 30; i++)
            {
                JObject jobject = new JObject();
                jobject["index"] = i + 1;

                jobject["data"] = rnd.Next(7000, 9000);
                jarray.Add(jobject);
            }
            return jarray;
        }


        [HttpGet]
        public JArray GetData2()
        {
            JArray jarray = new JArray();
            Random rnd = new Random();
            for (int i = 0; i < 30; i++)
            {
                JObject jobject = new JObject();
                jobject["index"] = i + 1;

                jobject["data"] = rnd.Next(7000, 9000);
                jarray.Add(jobject);
            }
            return jarray;
        }

         




    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值