asp.net mvc 页面转向及传参数

1RenderView

RenderView的重载:
RenderView(string viewName);
RenderView(string viewName, object viewData);
RenderView(string viewName, string masterName);
RenderView(string viewName, string masterName, object viewData);
我们常用的当然就是第一种
第二种RenderView(string viewName, object viewData);是在显示view时附加一个ViewData
如:
RenderView("Index", new
{
     name = "123",
     sex = 123

});

我们就可以在相应的View(即Index.aspx)中调用<%=ViewData["name"]%>来得到它的值
RenderView(string viewName, string masterName);
则是除了Viewname之外还指定了母板页

2.Redirect方法跳转页面到其它的Controller/Action

RedirectToAction(Action名);
RedirectToAction(Action名, Controller名);
RedirectToAction(RouteValueDictionary);

在这里前两种都没有什么好说的RedirectToAction("About","Home");就是一种写法
主要是第三种重载
用户可以这样写

             System.Web.Routing.RouteData routeData = new System.Web.Routing.RouteData();
             routeData.Values.Add(
" Action " , " About " );
             routeData.Values.Add(
" Controller " , " Home " );
             RedirectToAction(routeData.Values);


这样就可以完成页面跳转
当然,也可以使用传统的Response.Redirect来完成页面的跳转

3.Action 中 return View()的理解

也就是ActionResult,这里return View()其实就是返回html格式的内容给页面显示,而ActionResult是有很多类型的:

· View() – 返回一个 ViewResult.

· PartialView() – 返回一个 PartialViewResult.

· RedirectToAction() – 返回一个 RedirectToRouteResult .

· Redirect() – 返回一个 RedirectResult.

· Content() – 返回一个 ContentResult.

· Json() – 返回一个 JsonResult.

· File() – 返回一个 FileResult.

· JavaScript() – 返回一个 JavaScriptResult.

· RedirectToRoute() – 返回一个 RedirectToRouteResult.

解释:

· ViewResult – 表示一个普通的 ASP.NET MVC view.

· PartialViewResult – 表示一个ASP.NET MVC view的一个片段.

· RedirectResult – 表示重定向到另外一个controller action 或者 URL.

return RedirectToAction("Index"); return RedirectToAction(“Index”, “Product”);return RedirectToAction(“Details”, new {id=53});· ContentResult – 表示发送一些基本的类型的内容给浏览器,只要是.net的基本类型都可以 比如:string,int,double等等

public string SayHello()
{
       return "Hello";
}· JsonResult – 返回一个Json类型给浏览器


        public ActionResult List()
        {
            var quotes = new List<string>
            {
                "Look before you leap",
                "The early bird gets the worm",
                "All hat, no cattle"
            };
             
            return Json(quotes);
        }· FileResult – 返回一个文件,用来下载的

        public ActionResult Download()
        {
            
            return File("~/Content/CompanyPlans.docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "CompanyPlans.docx");
        }
· EmptyResult – 一个action没有返回结果

· HttpUnauthorizedResult – 返回HTTP Unauthorized status code.

· JavaScriptResult – 返回一个JavaScript 文件.

· RedirectToRouteResult – 使用route values重定向到另外一个controller的action

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值