asp.net MVC学习笔记三(第一个表单提交,第一个页面跳转及MVC中的ViewState与Session)
用惯WebForm的可能会忘掉了传统的表单提交方式,jsp,asp,php都是这样。MVC是返璞归真了。
用到的Model
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace FirstMVC.Models
{
/// <summary>
/// 用户实体类
/// </summary>
public class UserInfoModel
{
public string UserName { get; set; }
public string Password { get; set; }
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace FirstMVC.Models
{
/// <summary>
/// 用户实体类
/// </summary>
public class UserInfoModel
{
public string UserName { get; set; }
public string Password { get; set; }
}
}
用到的View,这里指定表单提交的路径,向哪个Action提交。
代码
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>UserView</title>
</head>
<body>
<div>
<p>
ViewState<%=ViewData["ViewState"] %><br />
Session<%=TempData["Session"] %>
</p>
<%--这里定义表单提交向哪里。跟传统的模式一样了。controller名字与action名字不区分大小写--%>
<%using(Html.BeginRouteForm(new {controller = "user",action="process"})){ %>
<%--表单数据,这里命名也不区分大小写--%>
<p><label>密码1:</label><%=Html.Password("a.password") %></p>
<p><label>用户名1:</label><%=Html.TextBox("a.username") %></p>
<p><label>密码2:</label><%=Html.Password("b.password") %></p>
<p><label>用户名2:</label><%=Html.TextBox("b.username") %></p>
<p><input type="submit" /></p>
<%} %>
</div>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>UserView</title>
</head>
<body>
<div>
<p>
ViewState<%=ViewData["ViewState"] %><br />
Session<%=TempData["Session"] %>
</p>
<%--这里定义表单提交向哪里。跟传统的模式一样了。controller名字与action名字不区分大小写--%>
<%using(Html.BeginRouteForm(new {controller = "user",action="process"})){ %>
<%--表单数据,这里命名也不区分大小写--%>
<p><label>密码1:</label><%=Html.Password("a.password") %></p>
<p><label>用户名1:</label><%=Html.TextBox("a.username") %></p>
<p><label>密码2:</label><%=Html.Password("b.password") %></p>
<p><label>用户名2:</label><%=Html.TextBox("b.username") %></p>
<p><input type="submit" /></p>
<%} %>
</div>
</body>
</html>
用到的Controller,这里的Process Action接收数据并页面跳转。
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FirstMVC.Models;
namespace FirstMVC.Controllers
{
public class UserController : Controller
{
/// <summary>
/// 辅助判断的方法
/// </summary>
/// <param name="userName"></param>
/// <param name="password"></param>
/// <returns></returns>
bool AreEquals(string userName, string password)
{
return (userName.ToLower() == "admin" && password.ToLower() == "admin123");
}
/// <summary>
/// 接收post提交的数据
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
public ActionResult Process(UserInfoModel a, UserInfoModel b)
{
if (AreEquals(a.UserName, a.Password) && AreEquals(b.UserName, b.Password))
return Content("登录成功");
else
return Redirect("http://www.0379zd.com");
}
//
// GET: /User/
public ActionResult UserView()
{
//ViewData[""]相当于ViewState[""]
ViewData["ViewState"] = "MVC ViewState";
return View();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FirstMVC.Models;
namespace FirstMVC.Controllers
{
public class UserController : Controller
{
/// <summary>
/// 辅助判断的方法
/// </summary>
/// <param name="userName"></param>
/// <param name="password"></param>
/// <returns></returns>
bool AreEquals(string userName, string password)
{
return (userName.ToLower() == "admin" && password.ToLower() == "admin123");
}
/// <summary>
/// 接收post提交的数据
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
public ActionResult Process(UserInfoModel a, UserInfoModel b)
{
if (AreEquals(a.UserName, a.Password) && AreEquals(b.UserName, b.Password))
return Content("登录成功");
else
return Redirect("http://www.0379zd.com");
}
//
// GET: /User/
public ActionResult UserView()
{
//ViewData[""]相当于ViewState[""]
ViewData["ViewState"] = "MVC ViewState";
return View();
}
}
}
首页登录的时候,设置TempData
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace FirstMVC.Controllers
{
[HandleError]
public class HomeController : Controller
{
public ActionResult Index()
{
ViewData["Message"] = "Welcome to ASP.NET MVC!";
//TempData[""]相当于Session[""]
TempData["Session"] = "MVC Session";
return View();
}
public ActionResult About()
{
return View();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace FirstMVC.Controllers
{
[HandleError]
public class HomeController : Controller
{
public ActionResult Index()
{
ViewData["Message"] = "Welcome to ASP.NET MVC!";
//TempData[""]相当于Session[""]
TempData["Session"] = "MVC Session";
return View();
}
public ActionResult About()
{
return View();
}
}
}
代码
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Home Page
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2><%= Html.Encode(ViewData["Message"]) %></h2>
<p>
第一个次表单提交,第一个页面跳转及MVC中的ViewState与Session<a href="/user/userview" title="ASP.NET MVC Website">http://asp.net/mvc</a>.
</p>
</asp:Content>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Home Page
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2><%= Html.Encode(ViewData["Message"]) %></h2>
<p>
第一个次表单提交,第一个页面跳转及MVC中的ViewState与Session<a href="/user/userview" title="ASP.NET MVC Website">http://asp.net/mvc</a>.
</p>
</asp:Content>
我这个博客废弃不用了,今天想寻找外链的时候,突然想到这个博客权重很高。
有需要免费外链的,留言即可,我准备把这个博客变成免费的友情链接站点。