MVC 【Razor 视图引擎】案例分析
- using MvcApplication1.Models;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- namespace MvcApplication1.Controllers
- {
- public class LoginController : Controller
- {
- //
- // GET: /Login/
- // 登录视图
- public ActionResult Index()
- {
- return View();
- }
- // 登录验证
- public ActionResult YanZheng(string uname, string pwd)
- {
- bool b = new UsersData().SelectUser(uname, pwd);
- if (b)
- {
- Response.Cookies["user"].Value = uname;
- Response.Cookies["user"].Expires = DateTime.Now.AddDays();
- return RedirectToAction("Index", "Home");
- }
- else
- {
- TempData["loginerror"] = "用户名密码错误";
- return RedirectToAction("Index", "Login");
- }
- }
- }
- }
登录 控制器
- @{
- Layout = null;
- }
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="viewport" content="width=device-width" />
- <title>Index</title>
- </head>
- <body>
- <div>
- @{using (Html.BeginForm("YanZheng", "Login"))
- {
- @:用户名:<input type="text" name="uname" /><br />
- @:密码:<input type="password" name="pwd" /><br />
- <input type="submit" value="登录" />
- if (TempData["loginerror"] != null)
- {
- <div style="color: red;">@TempData["loginerror"]</div>
- }
- }
- }
- </div>
- </body>
- </html>
登录 视图
--------------------------------------------
- using MvcApplication1.Models;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- namespace MvcApplication1.Controllers
- {
- public class HomeController : Controller
- {
- //
- // GET: /Home/
- //展示方法
- public ActionResult Index()
- {
- //判断是否登录,有登录信息展示
- if (Request.Cookies["user"] != null)
- {
- return View();
- }
- else
- { //无登录信息返回原视图
- return RedirectToAction("Index", "Login");
- }
- }
- //添加视图
- public ActionResult Insert()
- {
- return View();
- }
- //添加方法
- //界面上 name 取的名 与字段名一样 会自动拼成匹配的对象
- public ActionResult Insert1(Users u)
- {
- new UsersData().Insert(u);
- return RedirectToAction("Index");
- }
- //修改 接收传值
- public ActionResult Update(string id)
- {
- Users u = new UsersData().SelectUser(id);
- if (u != null)
- {
- return View(u);
- }
- else
- {
- return RedirectToAction("index", "Home");
- }
- }
- }
展示,修改,添加 控制器
- @{
- Layout = null;
- }
- @using MvcApplication1.Models;
- @*引用命名空间*@
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="viewport" content="width=device-width" />
- <title>Index</title>
- </head>
- <body>
- <div>
- <h1>这是Razor语法出来的视图主界面</h1>
- <table style="width: 100%; text-align: center; background-color: navy;">
- <tr style="color: white;">
- <td>用户名</td>
- <td>密码</td>
- <td>昵称</td>
- <td>性别</td>
- <td>生日</td>
- <td>民族</td>
- <td>操作</td>
- </tr>
- @{
- List<Users> ulist = new UsersData().SelectAll();
- foreach (Users u in ulist)
- {
- <tr style="background-color: white;">
- <td>@u.UserName</td>
- <td>@u.PassWord</td>
- <td>@(u.NickName)同学</td>
- <td>@(u.Sex == true ? "男" : "女")</td>
- <td>@u.Birthday.Value.ToString("yyyy年MM月dd日")</td>
- <td>@u.UserNation.NationName</td>
- <td>@Html.ActionLink("修改", "Update/" + u.Ids, "Home") </td>
- @*修改--在动作后面接 传的值*@
- </tr>
- }
- }
- </table>
- @Html.ActionLink("添加hehe", "Insert", "Home")
- @*使用超链接添加*@
- <input type="button" value="添加新用户" id="btn1" />
- @*使用普通按钮添加*@
- </div>
- </body>
- </html>
- <script type="text/javascript">
- document.getElementById("btn1").onclick = function () {
- window.location.href = "@Url.Action("Insert", "Home")";
- } @*使用普通按钮添加----方法*@
- </script>
展示 视图
- @{
- Layout = null;
- }
- @using MvcApplication1.Models;
- @*引用命名空间*@
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="viewport" content="width=device-width" />
- <title>Insert</title>
- </head>
- <body>
- <div>
- <h1>添加新用户</h1>
- @{ using (Html.BeginForm("Insert1", "Home", "post"))
- {
- @:用户名:<input type="text" name="username" /><br />
- @:密码:<input type="text" name="password" /><br />
- @:昵称:<input type="text" name="nickname" /><br />
- @:性别:
- <input type="radio" value="True" id="r1" name="sex" checked="checked" /><label for="r1">男</label>
- <input type="radio" value="False" id="r2" name="sex" /><label for="r2">女</label>
- <br />
- @:生日:<input type="text" name="birthday" /><br />
- @:民族:
- <select name="nation">
- @{
- List<UserNation> unlist = new UserNationData().SelectAll();
- foreach (UserNation un in unlist)
- {
- <option value="@un.NationCode">@un.NationName</option>
- }
- }
- </select>
- <br />
- <div id="div1" style="width: 100px; height: 100px; background-color: red;">保存</div>
- <input type="button" value="保存" id="btn1" />
- }
- }
- </div>
- </body>
- </html>
- <script type="text/javascript">
- document.getElementById("div1").onclick = function () {
- this.parentNode.submit();
- //点击div时 提交
- }
- document.getElementById("btn1").onclick = function () {
- this.form.submit();
- } //普通按钮提交
- </script>
添加 视图
- @{
- Layout = null;
- }
- @using MvcApplication1.Models;
- @*引用命名空间*@
- @model Users
- @* 声明传进来的强类型的数据类型*@
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="viewport" content="width=device-width" />
- <title>Update</title>
- </head>
- <body>
- <div>
- @{ using (Html.BeginForm("Update1", "Home", "post"))
- {
- @:用户名:<input type="text" disabled="disabled" name="username" value="@Model.UserName" /><br />
- @:密码:<input type="text" name="password" value="@Model.PassWord" /><br />
- @:昵称:<input type="text" name="nickname" value="@Model.NickName" /><br />
- @:性别:
- <input type="radio" value="True" id="r1" name="sex" @(Model.Sex == true ? "checked='checked'" : "") /><label for="r1">男</label>
- <input type="radio" value="False" id="r2" name="sex" @(Model.Sex == false ? "checked='checked'" : "") /><label for="r2">女</label>
- <br />
- @:生日:<input type="text" name="birthday" value="@Model.Birthday.Value" /><br />
- @:民族:
- <select name="nation">
- @{
- List<UserNation> unlist = new UserNationData().SelectAll();
- foreach (UserNation un in unlist)
- {
- <option @(un.NationCode == Model.Nation ? "selected='selected'" : "") value="@un.NationCode">@un.NationName</option>
- }
- }
- </select>
- <br />
- <div id="div1" style="width: 100px; height: 100px; background-color: red;">保存</div>
- //用 div 提交
- <input type="button" value="保存" id="btn1" />
- //用普通按钮提交
- }
- }
- </div>
- </body>
- </html>
- <script type="text/javascript">
- document.getElementById("div1").onclick = function () {
- this.parentNode.submit();
- //用 div 提交
- }
- document.getElementById("btn1").onclick = function () {
- this.form.submit();
- //用普通按钮提交
- }
- </script>
修改 视图
--------------
- @{
- Layout = null;
- }
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="viewport" content="width=device-width" />
- <title>Test1</title>
- <script src="~/js/jquery-1.7.1.min.js"></script>
- </head>
- <body>
- <div>
- <input type="text" id="txt1" name="t1" />
- <input type="button" value="验证" id="btn1" />
- </div>
- </body>
- </html>
- <script type="text/javascript">
- $("#btn1").click(function () {
- $.ajax({
- url: "/ajax/aaa.ashx", data: { "t": $("#txt1").val() }, type: "post", dataType: "json",
- success: function (msg) {
- if (msg.ok == "") {
- alert("可用");
- }
- else {
- alert("抱歉,已被占用!!");
- }
- }
- });
- });
- </script>
番外--用AJAX验证
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using MvcApplication1.Models;
- namespace MvcApplication1.ajax
- {
- /// <summary>
- /// aaa 的摘要说明
- /// </summary>
- public class aaa : IHttpHandler
- {
- public void ProcessRequest(HttpContext context)
- {
- string end = "{\"ok\":\"0\"}";
- string uname = context.Request["t"];
- using (DataClasses1DataContext con = new DataClasses1DataContext())
- {
- Users u = con.Users.Where(r => r.UserName == uname).FirstOrDefault();
- if (u != null)
- {
- end = "{\"ok\":\"1\"}";
- }
- }
- context.Response.Write(end);
- context.Response.End();
- }
- public bool IsReusable
- {
- get
- {
- return false;
- }
- }
- }
- }
验证
-- 各 方法未写
MVC 【Razor 视图引擎】案例分析的更多相关文章
- ASP.NET MVC——Razor视图引擎
Razor是MVC框架视图引擎,我们今天就来说一说Razor视图引擎. 首先还是来创建一个基础项目叫Razor来演示. 先来定义一个Model叫Product public class Product ...
- ASP.NET MVC Razor视图引擎攻略
--引子 看下面一段MVC 2.0的代码. <%if (Model != null){%> <p><%=Model%></p><%}%>&l ...
- ASP.NET WEB应用程序(.network4.5)MVC Razor视图引擎2 视图模板页
https://www.cnblogs.com/xlhblogs/archive/2013/06/09/3129449.html MVC Razor模板引擎 @RenderBody.@RenderPa ...
- MVC Razor视图引擎
Razor 不是编程语言.它是服务器端标记语言. Razor 是一种允许您向网页中嵌入基于服务器的代码(Visual Basic 和 C#)的标记语法 当网页被写入浏览器时,基于服务器的代码能够创建动 ...
- ASP.NET WEB应用程序(.network4.5)MVC Razor视图引擎2
https://www.bbsmax.com/A/gAJG67OXzZ/ 在MVC3.0版本的时候,微软终于引入了第二种模板引擎:Razor.在这之前,我们一直在使用WebForm时代沿留下来的ASP ...
- ASP.NET WEB应用程序(.network4.5)MVC Razor视图引擎2 HtmlHelper-超链接方法
一.@Html.ActionLink()概述 在MVC的Rasor视图引擎中,微软采用一种全新的方式来表示从前的超链接方式,它代替了从前的繁杂的超链接标签,让代码看起来更加简洁.通过浏览器依然会解析成 ...
- ASP.NET WEB应用程序(.network4.5)MVC Razor视图引擎2 动态数据的呈现
https://www.cnblogs.com/cynchanpin/p/7065098.html 在MVC3開始.视图数据能够通过ViewBag属性訪问.在MVC2中则是使用ViewData.MVC ...
- MVC Razor视图引擎的入门
首先我们来说说他的给我们开发者带来那些好处吧: Razor语法易于输入,易于阅读,微软当时是这样定义的:简洁,富有表现力和灵活性,支持所有文本编辑器,强大的智能提示功能,单元测试. Rozor文件类型 ...
- MVC Razor视图引擎控件
0.日期转化
- Asp.Net MVC Razor视图引擎与My97DatePicker插件的结合
using System; using System.Collections.Generic; using System.Runtime.CompilerServices; using System. ...
随机推荐
- ActiveMQ_6持久化
activemq持久化 ActiveMQ提供了插件式的消息存储,主要有有如下几种: 1.AMQ消息存储-基于文件的存储方式,是以前的默认消息存储 2.KahaDB消息存储-提供了容量的提升和恢复能力, ...
- ActiveMQ_4SpringBoot整合
SpringBoot实现 引入jar包 <dependency> <groupId>org.springframework.boot</groupId> ...
- SWPU-ACM集训队周赛之组队赛(3-11) E题题解
点这里去做题 %*c 读入时跳过一位,本题中即跳过"-"; #include<stdio.h> int run(int x) //判断闰年 { ; ==&&a ...
- noip第19课资料
- 【转】UniGUI Session管理說明
[转]UniGUI Session管理說明 (2015-12-29 15:41:15) 转载▼ 分类: uniGUI 台中cmj朋友在uniGUI中文社区QQ群里发布的,转贴至此. UniGUI ...
- Request参数值自动去空格
/// <summary> /// TypeTrimHelper /// </summary> public static class TypeTrimHelper { /// ...
- 《深入理解JAVA虚拟机》——学习笔记
JVM内存模型以及分区 JVM内存分为: 1.方法区:线程共享的区域,存储已经被虚拟机加载的类信息.常量.静态变量.即时编译器编译后的代码等数据 2.堆:线程共享的区域,存储对象实例,以及给数组分配的 ...
- jsapi微信支付
JSAPI微信支付 引用js <script type="text/javascript" src="http://res.wx.qq.com/open/js/jw ...
- 深圳scala-meetup-20180902(2)- Future vs Task and ReaderMonad依赖注入
在对上一次3月份的scala-meetup里我曾分享了关于Future在函数组合中的问题及如何用Monix.Task来替代.具体分析可以查阅这篇博文.在上篇示范里我们使用了Future来实现某种non ...
- 文件上传和WAF的攻与防
Author:JoyChouDate:20180613 1. 前言 本文的测试环境均为 nginx/1.10.3 PHP 5.5.34 有些特性和 语言及webserver有关,有问题的地方,欢迎大家 ...