MVC 之Action

一、指向视图和转向URL

View Code
        /// <summary>
        /// 指向MyView视图
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View("MyView");
        }

        /// <summary>
        /// 输入Derived/Redirect会转向Derived/Index
        /// </summary>
        /// <returns></returns>
        public ActionResult Redirect()
        {
            return new RedirectResult("/Derived/Index");            //return Redirect("/Derived/Index");效果一样
        }

 关于ViewResult的几个重载方法有如下几个:

View Code

        //
        // 摘要:
        //     创建一个将视图呈现给响应的 System.Web.Mvc.ViewResult 对象。
        //
        // 返回结果:
        //     将视图呈现给响应的视图结果。
        protected internal ViewResult View();
        //
        // 摘要:
        //     创建一个呈现指定的 System.Web.Mvc.IView 对象的 System.Web.Mvc.ViewResult 对象。
        //
        // 参数:
        //   view:
        //     为响应呈现的视图。
        //
        // 返回结果:
        //     视图结果。
        protected internal ViewResult View(IView view);
        //
        // 摘要:
        //     使用模型创建一个将视图呈现给响应的 System.Web.Mvc.ViewResult 对象。
        //
        // 参数:
        //   model:
        //     视图呈现的模型。
        //
        // 返回结果:
        //     视图结果。
        protected internal ViewResult View(object model);
        //
        // 摘要:
        //     使用视图名称创建一个呈现视图的 System.Web.Mvc.ViewResult 对象。
        //
        // 参数:
        //   viewName:
        //     为响应呈现的视图的名称。
        //
        // 返回结果:
        //     视图结果。
        protected internal ViewResult View(string viewName);
        //
        // 摘要:
        //     创建一个呈现指定的 System.Web.Mvc.IView 对象的 System.Web.Mvc.ViewResult 对象。
        //
        // 参数:
        //   view:
        //     为响应呈现的视图。
        //
        //   model:
        //     视图呈现的模型。
        //
        // 返回结果:
        //     视图结果。
        protected internal virtual ViewResult View(IView view, object model);
        //
        // 摘要:
        //     使用视图名称和模型创建一个将视图呈现给响应的 System.Web.Mvc.ViewResult 对象。
        //
        // 参数:
        //   viewName:
        //     为响应呈现的视图的名称。
        //
        //   model:
        //     视图呈现的模型。
        //
        // 返回结果:
        //     视图结果。
        protected internal ViewResult View(string viewName, object model);
        //
        // 摘要:
        //     使用视图名称和母版页名称创建一个将视图呈现给响应的 System.Web.Mvc.ViewResult 对象。
        //
        // 参数:
        //   viewName:
        //     为响应呈现的视图的名称。
        //
        //   masterName:
        //     在呈现视图时要使用的母版页或模板的名称。
        //
        // 返回结果:
        //     视图结果。
        protected internal ViewResult View(string viewName, string masterName);
        //
        // 摘要:
        //     使用视图名称、母版页名称和模型创建一个呈现视图的 System.Web.Mvc.ViewResult 对象。
        //
        // 参数:
        //   viewName:
        //     为响应呈现的视图的名称。
        //
        //   masterName:
        //     在呈现视图时要使用的母版页或模板的名称。
        //
        //   model:
        //     视图呈现的模型。
        //
        // 返回结果:
        //     视图结果。
        protected internal virtual ViewResult View(string viewName, string masterName, object model);

二、RenderAction与RenderPartial

上面的两个都是用来添加部分视图的,通常作为模板的一部分,或者被替换的部分。本节主要是介绍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();

运行的结果如下:,下面通过一张图来说明二者的区别:

 三、FileResult

文件下载代码:

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

 四、HttpNotFound

        public HttpStatusCodeResult StatusCode()
        {
            return HttpNotFound();
        }

五、jsonResult

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

代码偏多,理论较少。

posted @ 2013-04-21 18:06  haiziguo  阅读(4535)  评论(11编辑  收藏  举报