MVC 【Razor 视图引擎】案例分析
2021-07-02 06:05
阅读:513
YPE html>
标签:public where pac sse 超链接 razor语法 验证 select result
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(3); return RedirectToAction("Index", "Home"); } else { TempData["loginerror"] = "用户名密码错误"; return RedirectToAction("Index", "Login"); } } } }
@{ Layout = null; } "viewport" content="width=device-width" />Index @{using (Html.BeginForm("YanZheng", "Login")) { @:用户名:"text" name="uname" />
@:密码:"password" name="pwd" />
"submit" value="登录" /> if (TempData["loginerror"] != null) {"color: red;">@TempData["loginerror"]} } }
--------------------------------------------
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; @*引用命名空间*@ "viewport" content="width=device-width" />Index 这是Razor语法出来的视图主界面
"width: 100%; text-align: center; background-color: navy;">
@Html.ActionLink("添加hehe", "Insert", "Home") @*使用超链接添加*@ "button" value="添加新用户" id="btn1" /> @*使用普通按钮添加*@"color: white;"> @{ List用户名 密码 昵称 性别 生日 民族 操作 ulist = new UsersData().SelectAll(); foreach (Users u in ulist) { "background-color: white;"> } }@u.UserName @u.PassWord @(u.NickName)同学 @(u.Sex == true ? "男" : "女") @u.Birthday.Value.ToString("yyyy年MM月dd日") @u.UserNation.NationName @Html.ActionLink("修改", "Update/" + u.Ids, "Home") @*修改--在动作后面接 传的值*@
@{ Layout = null; } @using MvcApplication1.Models; @*引用命名空间*@ "viewport" content="width=device-width" />Insert 添加新用户
@{ using (Html.BeginForm("Insert1", "Home", "post")) { @:用户名:"text" name="username" />
@:密码:"text" name="password" />
@:昵称:"text" name="nickname" />
@:性别: "radio" value="True" id="r1" name="sex" checked="checked" /> "radio" value="False" id="r2" name="sex" />
@:生日:"text" name="birthday" />
@:民族: select name="nation"> @{ Listunlist = new UserNationData().SelectAll(); foreach (UserNation un in unlist) { } } select> "div1" style="width: 100px; height: 100px; background-color: red;">保存"button" value="保存" id="btn1" /> } }
@{ Layout = null; } @using MvcApplication1.Models; @*引用命名空间*@ @model Users @* 声明传进来的强类型的数据类型*@ "viewport" content="width=device-width" />Update @{ using (Html.BeginForm("Update1", "Home", "post")) { @:用户名:"text" disabled="disabled" name="username" value="@Model.UserName" />
@:密码:"text" name="password" value="@Model.PassWord" />
@:昵称:"text" name="nickname" value="@Model.NickName" />
@:性别: "radio" value="True" id="r1" name="sex" @(Model.Sex == true ? "checked=‘checked‘" : "") /> "radio" value="False" id="r2" name="sex" @(Model.Sex == false ? "checked=‘checked‘" : "") />
@:生日:"text" name="birthday" value="@Model.Birthday.Value" />
@:民族: select name="nation"> @{ Listunlist = new UserNationData().SelectAll(); foreach (UserNation un in unlist) { } } select> "div1" style="width: 100px; height: 100px; background-color: red;">保存//用 div 提交 "button" value="保存" id="btn1" /> //用普通按钮提交 } }
--------------
@{ Layout = null; } "viewport" content="width=device-width" />Test1 "text" id="txt1" name="t1" /> "button" value="验证" id="btn1" />
using System; using System.Collections.Generic; using System.Linq; using System.Web; using MvcApplication1.Models; namespace MvcApplication1.ajax { ////// aaa 的摘要说明 /// 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 视图引擎】案例分析
标签:public where pac sse 超链接 razor语法 验证 select result
原文地址:http://www.cnblogs.com/Tanghongchang/p/7128573.html
评论
亲,登录后才可以留言!