View Code关于ViewResult的几个重载方法有如下几个:
View Code上面的两个都是用来添加部分视图的,通常作为模板的一部分,或者被替换的部分。本节主要是介绍ResultAction,所以先来看RenderAction。我想把所有页面的顶部显示为我想要的视图。
添加一个Controller取名MenuController;方法为Sum,代码如下:
        public PartialViewResult Sum()
        {
            int[] ArryPrice = { 1, 2, 3 };
            IEnumerable<int> priceList = ArryPrice;
            return PartialView(priceList);
        }
然后在新建一个部分视图Sum.cshtml:
@model IEnumerable<int> @Model.Sum().ToString();
在模板页_Layout.cshtml添加一个<h1 style=" background:red">总价为:@{Html.RenderAction("Sum", "Menu");}</h1>。
如果是不想让sum方法被其他的非Html.RenderAction方法访问,那么在sum方法前面加个[ChildActionOnly]标签。
除此之外,还有对应的还有个是@{Html.RenderPartial("部分视图的名称或者是路径+部分视图名");}方法。该方法是不需要重新请求Action。
新建给一个controller名称为DerivedController,然后添加个List方法,代码如下:
        public ActionResult List()
        {
            object str = "List Test";
            return View(str);
        }
然后添加List对应的视图,然后添加一个Partial.cshtml视图,其代码如下:
/*List视图代码*/
@model string
@{
    ViewBag.Title = "List";
}
@{Html.RenderPartial("Partial");}
<h2>List</h2>
/*Partial视图代码*/
@model string
@Model.ToString().ToUpper();
运行的结果如下:
,下面通过一张图来说明二者的区别:

文件下载代码:
        /// <summary>
        /// 下载文件
        /// </summary>
        /// <returns></returns>
        public FileResult AnnualReport()
        {
           //下载文件名称
            string filename = @"c:\test.txt";
            //文件类型
            string contentType = "application/txt";
            //下载文件名
            string downloadName = "txt.txt";
            return File(filename, contentType, downloadName);
        }
        public HttpStatusCodeResult StatusCode()
        {
            return HttpNotFound();
        }
        public JsonResult JsonTest()
        {
            List<Person> list = new List<Person>() { new Person() { Name = "张三", Age = 28 }, new Person() { Name = "李四", Age = 28 } };
            var result=JsonHelper.JsonSerializer<List<Person>>(list);
            return Json(result, JsonRequestBehavior.AllowGet);
        }
小结,以上是一些常用的action,除此之外还有下面一个也是比较常用的、
public RedirectToRouteResult Redirect() {
return RedirectToAction("Index");
}
代码偏多,理论较少。
原文:http://www.cnblogs.com/hzxcms/p/4279080.html