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;
}
}
}