1. using MvcApplication1.Models;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.Mvc;
  7.  
  8. namespace MvcApplication1.Controllers
  9. {
  10. public class LoginController : Controller
  11. {
  12. //
  13. // GET: /Login/
  14.  
  15. // 登录视图
  16. public ActionResult Index()
  17. {
  18. return View();
  19. }
  20.  
  21. // 登录验证
  22. public ActionResult YanZheng(string uname, string pwd)
  23. {
  24.  
  25. bool b = new UsersData().SelectUser(uname, pwd);
  26. if (b)
  27. {
  28. Response.Cookies["user"].Value = uname;
  29. Response.Cookies["user"].Expires = DateTime.Now.AddDays();
  30.  
  31. return RedirectToAction("Index", "Home");
  32. }
  33. else
  34. {
  35. TempData["loginerror"] = "用户名密码错误";
  36. return RedirectToAction("Index", "Login");
  37. }
  38.  
  39. }
  40.  
  41. }
  42. }

登录 控制器

  1. @{
  2. Layout = null;
  3. }
  4.  
  5. <!DOCTYPE html>
  6.  
  7. <html>
  8. <head>
  9. <meta name="viewport" content="width=device-width" />
  10. <title>Index</title>
  11. </head>
  12. <body>
  13. <div>
  14. @{using (Html.BeginForm("YanZheng", "Login"))
  15. {
  16. @:用户名:<input type="text" name="uname" /><br />
  17. @:密码:<input type="password" name="pwd" /><br />
  18. <input type="submit" value="登录" />
  19. if (TempData["loginerror"] != null)
  20. {
  21. <div style="color: red;">@TempData["loginerror"]</div>
  22. }
  23. }
  24. }
  25. </div>
  26. </body>
  27. </html>

登录 视图

--------------------------------------------

  1. using MvcApplication1.Models;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.Mvc;
  7.  
  8. namespace MvcApplication1.Controllers
  9. {
  10. public class HomeController : Controller
  11. {
  12. //
  13. // GET: /Home/
  14.  
  15. //展示方法
  16. public ActionResult Index()
  17. {
  18. //判断是否登录,有登录信息展示
  19. if (Request.Cookies["user"] != null)
  20. {
  21. return View();
  22. }
  23. else
  24. { //无登录信息返回原视图
  25. return RedirectToAction("Index", "Login");
  26. }
  27. }
  28.  
  29. //添加视图
  30. public ActionResult Insert()
  31. {
  32. return View();
  33. }
  34.  
  35. //添加方法
  36. //界面上 name 取的名 与字段名一样 会自动拼成匹配的对象
  37. public ActionResult Insert1(Users u)
  38. {
  39. new UsersData().Insert(u);
  40. return RedirectToAction("Index");
  41. }
  42.  
  43. //修改 接收传值
  44. public ActionResult Update(string id)
  45. {
  46. Users u = new UsersData().SelectUser(id);
  47. if (u != null)
  48. {
  49. return View(u);
  50. }
  51. else
  52. {
  53. return RedirectToAction("index", "Home");
  54. }
  55. }
  56.  
  57. }

展示,修改,添加 控制器

  1. @{
  2. Layout = null;
  3. }
  4. @using MvcApplication1.Models;
  5. @*引用命名空间*@
  6.  
  7. <!DOCTYPE html>
  8.  
  9. <html>
  10. <head>
  11. <meta name="viewport" content="width=device-width" />
  12. <title>Index</title>
  13. </head>
  14. <body>
  15. <div>
  16. <h1>这是Razor语法出来的视图主界面</h1>
  17.  
  18. <table style="width: 100%; text-align: center; background-color: navy;">
  19. <tr style="color: white;">
  20. <td>用户名</td>
  21. <td>密码</td>
  22. <td>昵称</td>
  23. <td>性别</td>
  24. <td>生日</td>
  25. <td>民族</td>
  26. <td>操作</td>
  27. </tr>
  28.  
  29. @{
  30. List<Users> ulist = new UsersData().SelectAll();
  31.  
  32. foreach (Users u in ulist)
  33. {
  34.  
  35. <tr style="background-color: white;">
  36. <td>@u.UserName</td>
  37. <td>@u.PassWord</td>
  38. <td>@(u.NickName)同学</td>
  39. <td>@(u.Sex == true ? "男" : "女")</td>
  40. <td>@u.Birthday.Value.ToString("yyyy年MM月dd日")</td>
  41. <td>@u.UserNation.NationName</td>
  42.  
  43. <td>@Html.ActionLink("修改", "Update/" + u.Ids, "Home") </td>
  44. @*修改--在动作后面接 传的值*@
  45. </tr>
  46. }
  47. }
  48.  
  49. </table>
  50. @Html.ActionLink("添加hehe", "Insert", "Home")
  51. @*使用超链接添加*@
  52.  
  53. <input type="button" value="添加新用户" id="btn1" />
  54. @*使用普通按钮添加*@
  55. </div>
  56. </body>
  57. </html>
  58.  
  59. <script type="text/javascript">
  60.  
  61. document.getElementById("btn1").onclick = function () {
  62. window.location.href = "@Url.Action("Insert", "Home")";
  63.  
  64. } @*使用普通按钮添加----方法*@
  65.  
  66. </script>

展示 视图

  1. @{
  2. Layout = null;
  3. }
  4. @using MvcApplication1.Models;
  5. @*引用命名空间*@
  6.  
  7. <!DOCTYPE html>
  8.  
  9. <html>
  10. <head>
  11. <meta name="viewport" content="width=device-width" />
  12. <title>Insert</title>
  13. </head>
  14. <body>
  15. <div>
  16. <h1>添加新用户</h1>
  17.  
  18. @{ using (Html.BeginForm("Insert1", "Home", "post"))
  19.  
  20. {
  21.  
  22. @:用户名:<input type="text" name="username" /><br />
  23. @:密码:<input type="text" name="password" /><br />
  24. @:昵称:<input type="text" name="nickname" /><br />
  25. @:性别:
  26. <input type="radio" value="True" id="r1" name="sex" checked="checked" /><label for="r1">男</label>
  27. <input type="radio" value="False" id="r2" name="sex" /><label for="r2">女</label>
  28. <br />
  29. @:生日:<input type="text" name="birthday" /><br />
  30. @:民族:
  31. <select name="nation">
  32. @{
  33. List<UserNation> unlist = new UserNationData().SelectAll();
  34.  
  35. foreach (UserNation un in unlist)
  36. {
  37. <option value="@un.NationCode">@un.NationName</option>
  38. }
  39.  
  40. }
  41. </select>
  42. <br />
  43.  
  44. <div id="div1" style="width: 100px; height: 100px; background-color: red;">保存</div>
  45.  
  46. <input type="button" value="保存" id="btn1" />
  47.  
  48. }
  49. }
  50.  
  51. </div>
  52. </body>
  53. </html>
  54. <script type="text/javascript">
  55. document.getElementById("div1").onclick = function () {
  56. this.parentNode.submit();
  57. //点击div时 提交
  58.  
  59. }
  60. document.getElementById("btn1").onclick = function () {
  61. this.form.submit();
  62.  
  63. } //普通按钮提交
  64.  
  65. </script>

添加 视图

  1. @{
  2. Layout = null;
  3. }
  4. @using MvcApplication1.Models;
  5. @*引用命名空间*@
  6.  
  7. @model Users
  8. @* 声明传进来的强类型的数据类型*@
  9.  
  10. <!DOCTYPE html>
  11.  
  12. <html>
  13. <head>
  14. <meta name="viewport" content="width=device-width" />
  15. <title>Update</title>
  16. </head>
  17. <body>
  18. <div>
  19. @{ using (Html.BeginForm("Update1", "Home", "post"))
  20. {
  21. @:用户名:<input type="text" disabled="disabled" name="username" value="@Model.UserName" /><br />
  22. @:密码:<input type="text" name="password" value="@Model.PassWord" /><br />
  23. @:昵称:<input type="text" name="nickname" value="@Model.NickName" /><br />
  24. @:性别:
  25.  
  26. <input type="radio" value="True" id="r1" name="sex" @(Model.Sex == true ? "checked='checked'" : "") /><label for="r1">男</label>
  27. <input type="radio" value="False" id="r2" name="sex" @(Model.Sex == false ? "checked='checked'" : "") /><label for="r2">女</label>
  28.  
  29. <br />
  30. @:生日:<input type="text" name="birthday" value="@Model.Birthday.Value" /><br />
  31. @:民族:
  32. <select name="nation">
  33. @{
  34. List<UserNation> unlist = new UserNationData().SelectAll();
  35.  
  36. foreach (UserNation un in unlist)
  37. {
  38. <option @(un.NationCode == Model.Nation ? "selected='selected'" : "") value="@un.NationCode">@un.NationName</option>
  39. }
  40.  
  41. }
  42. </select>
  43. <br />
  44.  
  45. <div id="div1" style="width: 100px; height: 100px; background-color: red;">保存</div>
  46. //用 div 提交
  47.  
  48. <input type="button" value="保存" id="btn1" />
  49. //用普通按钮提交
  50.  
  51. }
  52. }
  53. </div>
  54. </body>
  55. </html>
  56. <script type="text/javascript">
  57. document.getElementById("div1").onclick = function () {
  58. this.parentNode.submit();
  59. //用 div 提交
  60. }
  61. document.getElementById("btn1").onclick = function () {
  62. this.form.submit();
  63. //用普通按钮提交
  64. }
  65.  
  66. </script>

修改 视图

--------------

  1. @{
  2. Layout = null;
  3. }
  4.  
  5. <!DOCTYPE html>
  6.  
  7. <html>
  8. <head>
  9. <meta name="viewport" content="width=device-width" />
  10. <title>Test1</title>
  11. <script src="~/js/jquery-1.7.1.min.js"></script>
  12. </head>
  13. <body>
  14. <div>
  15.  
  16. <input type="text" id="txt1" name="t1" />
  17. <input type="button" value="验证" id="btn1" />
  18.  
  19. </div>
  20. </body>
  21. </html>
  22.  
  23. <script type="text/javascript">
  24.  
  25. $("#btn1").click(function () {
  26. $.ajax({
  27. url: "/ajax/aaa.ashx", data: { "t": $("#txt1").val() }, type: "post", dataType: "json",
  28. success: function (msg) {
  29. if (msg.ok == "") {
  30. alert("可用");
  31. }
  32. else {
  33. alert("抱歉,已被占用!!");
  34. }
  35.  
  36. }
  37. });
  38.  
  39. });
  40.  
  41. </script>

番外--用AJAX验证

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using MvcApplication1.Models;
  6.  
  7. namespace MvcApplication1.ajax
  8. {
  9. /// <summary>
  10. /// aaa 的摘要说明
  11. /// </summary>
  12. public class aaa : IHttpHandler
  13. {
  14.  
  15. public void ProcessRequest(HttpContext context)
  16. {
  17. string end = "{\"ok\":\"0\"}";
  18. string uname = context.Request["t"];
  19.  
  20. using (DataClasses1DataContext con = new DataClasses1DataContext())
  21. {
  22. Users u = con.Users.Where(r => r.UserName == uname).FirstOrDefault();
  23. if (u != null)
  24. {
  25. end = "{\"ok\":\"1\"}";
  26.  
  27. }
  28. }
  29. context.Response.Write(end);
  30. context.Response.End();
  31. }
  32.  
  33. public bool IsReusable
  34. {
  35. get
  36. {
  37. return false;
  38. }
  39. }
  40. }
  41. }

验证

-- 各 方法未写

MVC 【Razor 视图引擎】案例分析的更多相关文章

  1. ASP.NET MVC——Razor视图引擎

    Razor是MVC框架视图引擎,我们今天就来说一说Razor视图引擎. 首先还是来创建一个基础项目叫Razor来演示. 先来定义一个Model叫Product public class Product ...

  2. ASP.NET MVC Razor视图引擎攻略

    --引子 看下面一段MVC 2.0的代码. <%if (Model != null){%> <p><%=Model%></p><%}%>&l ...

  3. ASP.NET WEB应用程序(.network4.5)MVC Razor视图引擎2 视图模板页

    https://www.cnblogs.com/xlhblogs/archive/2013/06/09/3129449.html MVC Razor模板引擎 @RenderBody.@RenderPa ...

  4. MVC Razor视图引擎

    Razor 不是编程语言.它是服务器端标记语言. Razor 是一种允许您向网页中嵌入基于服务器的代码(Visual Basic 和 C#)的标记语法 当网页被写入浏览器时,基于服务器的代码能够创建动 ...

  5. ASP.NET WEB应用程序(.network4.5)MVC Razor视图引擎2

    https://www.bbsmax.com/A/gAJG67OXzZ/ 在MVC3.0版本的时候,微软终于引入了第二种模板引擎:Razor.在这之前,我们一直在使用WebForm时代沿留下来的ASP ...

  6. ASP.NET WEB应用程序(.network4.5)MVC Razor视图引擎2 HtmlHelper-超链接方法

    一.@Html.ActionLink()概述 在MVC的Rasor视图引擎中,微软采用一种全新的方式来表示从前的超链接方式,它代替了从前的繁杂的超链接标签,让代码看起来更加简洁.通过浏览器依然会解析成 ...

  7. ASP.NET WEB应用程序(.network4.5)MVC Razor视图引擎2 动态数据的呈现

    https://www.cnblogs.com/cynchanpin/p/7065098.html 在MVC3開始.视图数据能够通过ViewBag属性訪问.在MVC2中则是使用ViewData.MVC ...

  8. MVC Razor视图引擎的入门

    首先我们来说说他的给我们开发者带来那些好处吧: Razor语法易于输入,易于阅读,微软当时是这样定义的:简洁,富有表现力和灵活性,支持所有文本编辑器,强大的智能提示功能,单元测试. Rozor文件类型 ...

  9. MVC Razor视图引擎控件

    0.日期转化

  10. Asp.Net MVC Razor视图引擎与My97DatePicker插件的结合

    using System; using System.Collections.Generic; using System.Runtime.CompilerServices; using System. ...

随机推荐

  1. ActiveMQ_6持久化

    activemq持久化 ActiveMQ提供了插件式的消息存储,主要有有如下几种: 1.AMQ消息存储-基于文件的存储方式,是以前的默认消息存储 2.KahaDB消息存储-提供了容量的提升和恢复能力, ...

  2. ActiveMQ_4SpringBoot整合

    SpringBoot实现 引入jar包 <dependency>        <groupId>org.springframework.boot</groupId> ...

  3. SWPU-ACM集训队周赛之组队赛(3-11) E题题解

    点这里去做题 %*c  读入时跳过一位,本题中即跳过"-"; #include<stdio.h> int run(int x) //判断闰年 { ; ==&&a ...

  4. noip第19课资料

  5. 【转】UniGUI Session管理說明

    [转]UniGUI Session管理說明 (2015-12-29 15:41:15) 转载▼   分类: uniGUI 台中cmj朋友在uniGUI中文社区QQ群里发布的,转贴至此. UniGUI ...

  6. Request参数值自动去空格

    /// <summary> /// TypeTrimHelper /// </summary> public static class TypeTrimHelper { /// ...

  7. 《深入理解JAVA虚拟机》——学习笔记

    JVM内存模型以及分区 JVM内存分为: 1.方法区:线程共享的区域,存储已经被虚拟机加载的类信息.常量.静态变量.即时编译器编译后的代码等数据 2.堆:线程共享的区域,存储对象实例,以及给数组分配的 ...

  8. jsapi微信支付

    JSAPI微信支付 引用js <script type="text/javascript" src="http://res.wx.qq.com/open/js/jw ...

  9. 深圳scala-meetup-20180902(2)- Future vs Task and ReaderMonad依赖注入

    在对上一次3月份的scala-meetup里我曾分享了关于Future在函数组合中的问题及如何用Monix.Task来替代.具体分析可以查阅这篇博文.在上篇示范里我们使用了Future来实现某种non ...

  10. 文件上传和WAF的攻与防

    Author:JoyChouDate:20180613 1. 前言 本文的测试环境均为 nginx/1.10.3 PHP 5.5.34 有些特性和 语言及webserver有关,有问题的地方,欢迎大家 ...