用户管理实例 之 添加、查询
生活随笔
收集整理的這篇文章主要介紹了
用户管理实例 之 添加、查询
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
訪問(wèn)視圖先是訪問(wèn)控制器中的方法,在去找跟方法同名的視圖,或者指定的視圖
這里是用LinQ查詢數(shù)據(jù)庫(kù)的一張表的數(shù)據(jù),在控制器中ViewData存儲(chǔ)數(shù)據(jù),把此數(shù)據(jù)(一張表)在前臺(tái)頁(yè)面上顯示出來(lái)。
控制器中:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using MVCDemo.Models; //LinQ to SQL 類(lèi)在Model中,要在這里拿數(shù)據(jù)namespace MVCDemo.Controllers {public class UserInfoController : Controller{//// GET: /UserInfo/NorthwindDBDataContext dc = new NorthwindDBDataContext();/// <summary>/// 顯示數(shù)據(jù)的視圖/// </summary>/// <returns></returns>public ActionResult Index(){ViewData["data"] = dc.Users.AsEnumerable<Users>();return View();}/// <summary>/// 添加用戶的試圖/// </summary>/// <returns></returns>public ActionResult Add(){return View();}/// <summary>/// 添加的具體方法/// </summary>/// <returns></returns>public ActionResult ProcessAdd(){//通過(guò)表單post獲取值string name = Request.Form["name"].ToString();string pwd = Request.Form["pwd"].ToString();//string sex = Request["sex"];bool sex =Request.Form["sex"]=="on"?true:false; //復(fù)選框控件,勾了顯示 OnUsers user = new Users() { UserName = name, UserPwd = pwd, Sex = sex }; //實(shí)例一個(gè)用戶dc.Users.InsertOnSubmit(user); //添加數(shù)據(jù)庫(kù)中dc.SubmitChanges(); //更新數(shù)據(jù)庫(kù)return RedirectToAction("Index"); //返回到指定試圖 }} }視圖:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %><%@ Import Namespace="MVCDemo.Models" %> <%--引用命名空間--%> <!DOCTYPE html> <html> <head runat="server"><meta name="viewport" content="width=device-width" /><title>Index</title><style type="text/css">.auto-style1 {width: 400px;background-color: #FFFF00;}table tr{background-color:#fff;}</style> </head> <body><% IEnumerable<Users> list = ViewData["data"] as IEnumerable<Users>; //把object轉(zhuǎn)成可以被遍歷的集合 %> <%--寫(xiě)后臺(tái)代碼--%><div><table class="auto-style1"><tr><td>ID</td><td>UserName</td><td>Pwd</td><td>Sex</td></tr><% foreach (Users item in list){%><tr><td><%=item.ID %></td> <%--綁定數(shù)據(jù)--%><td><%=item.UserName %></td><td><%=item.UserPwd %></td><td><%=item.Sex %></td></tr><%} %></table></div> </body> </html>添加用戶信息頁(yè)面:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %><!DOCTYPE html><html> <head runat="server"><meta name="viewport" content="width=device-width" /><title>Add</title><style type="text/css">.auto-style1 {width:400px;}</style> </head> <body><form action="/UserInfo/ProcessAdd" method="post"> //點(diǎn)提交后 去到控制器為UseInfo下的ProcessAdd 方法<table class="auto-style1"><tr><td>名稱(chēng):</td><td><input type="text" name="name" /></td></tr><tr><td>密碼:</td><td><input type="text" name="pwd" /></td></tr><tr><td>性別:</td><td><input type="checkbox" name="sex" />男 </td></tr><tr ><td colspan="2"><input type="submit" value="添加用戶" /></td> </tr></table></form> </body> </html>?
轉(zhuǎn)載于:https://www.cnblogs.com/Sea1ee/p/5958100.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的用户管理实例 之 添加、查询的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: [ACM训练] ACM中巧用文件的输入输
- 下一篇: 硬链接和软连接(符号链接)