c# MVC 跳转页面
2014-08-21 14:21
671 查看
1)在VS中创建ASP.NET MVC 4 Web Application,在Project Template中选择Web API。
首先在Views\Home\中添加Add.aspx和Index.aspx
接下来我们将看的是系统默认首页Index.aspx和从Index.aspx跳转到Add.aspx
要看系统默认首页我们得看文件App_Start\RouteConfig.cs
那我们就来看看Controllers\HomeController.cs
接下来我们来看看是如何通过Javascript来跳转页面的下面是Views\Home|Index.aspx页面的代码:
当单击<a>或者<input>系统将跳转到Controllers\HomeController.cs中的Add方法,然后再跳转到Views\Home\Add.aspx页面。
首先在Views\Home\中添加Add.aspx和Index.aspx
接下来我们将看的是系统默认首页Index.aspx和从Index.aspx跳转到Add.aspx
要看系统默认首页我们得看文件App_Start\RouteConfig.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Routing; namespace MvcApplication15 { public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); } } }从这段代码中我相信有代码基础的你不难看出来系统启动后默认调用是controller = "Home", action = "Index",
那我们就来看看Controllers\HomeController.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace MvcApplication15.Controllers { public class HomeController : Controller { //对应Views\Home\Index.aspx public ActionResult Index() { return View(); } //对应Views\Home\Add.aspx public ActionResult Add() { return View(); } } }相信大家看到这段代码后恍然大悟,系统将调用Views\Home\Index.aspx页面(个人认为是通过系统文件名来联系的)。
接下来我们来看看是如何通过Javascript来跳转页面的下面是Views\Home|Index.aspx页面的代码:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %> <!DOCTYPE html> <html> <head runat="server"> <meta name="viewport" content="width=device-width" /> <title>Home Index</title> <script type="text/javascript" src="../../Scripts/jquery-1.7.1.min.js"></script> <script type="text/javascript"> $(function () { //js跳转页面(/controller/action) $('#jsTest').click(function () { window.location.href = '/Home/Add'; }); }); </script> </head> <body> <div> <%--html跳转(/controller/action)--%> <a href="/Home/Add">Home Add</a> <input id="jsTest" type="button" value="Home Add" /> </div> </body> </html>
当单击<a>或者<input>系统将跳转到Controllers\HomeController.cs中的Add方法,然后再跳转到Views\Home\Add.aspx页面。
相关文章推荐
- JAVASCRIPT实现的WEB页面跳转以及页面间传值方法
- (4.4.1)使用storyboard实现页面跳转,简单的数据传递
- 页面跳转 和虚拟键盘的自动隐藏
- 页面跳转方法总结大全
- js 页面跳转保存状态
- from表单实现无跳转上传文件,接收页面后台数据
- jsp 调用其他jsp页面 跳转
- spring mvc 页面跳转 携带数据的两种方式
- ZK页面跳转
- 解决页面跳转 返回刷新还是不刷新问题
- springboot中出现无法跳转到前台JSP页面
- 新发现原来documenet.URL也可以实现页面跳转
- Asp的实现页面跳转
- struts2使用action跳转后的页面路径如果是相对路径的话会出现css、js以及图片等无法加载问题
- JS 控制页面超时后自动跳转到登陆页面
- href携带参数跳转页面
- PHP下拉按钮跳转页面
- WUP 页面跳转过度动画
- jquery mobile ajax页面跳转出现(error load page的错误)及js与css失效的说明和处理
- js实现页面滚动切换导航栏/点击导航栏跳转到指定位置