项目一 :在界面输出 helloworld
controllers
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using mvc1.Models;namespace mvc1.Controllers{ public class HomeController : Controller { //action public string Index() { return "Hello World!!"; } public string test1() { return "这里是Test1,呵呵
"; } public string test2() { string s = ""; Listulist = new UsersData().SelectAll(); foreach (Users u in ulist) { s += u.NickName + " | "; } return s; } }}
Models
using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace mvc1.Models{ public class UsersData { ////// 返回全部Users表数据 /// ///public List SelectAll() { using (DataClasses1DataContext con = new DataClasses1DataContext()) { return con.Users.ToList(); } } }}
Views
项目二:
controllers层:
using mvc2.Models;using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;namespace mvc2.Controllers{ public class HomeController : Controller { public ActionResult Index() { return View(); } public ActionResult Insert() { return View(); } public ActionResult Insert1(string username, string password, string nickname, string sex, string birthday, string nation) { Users u = new Users(); u.UserName = username; u.PassWord = password; u.NickName = nickname; u.Sex = Convert.ToBoolean(sex); u.Birthday = Convert.ToDateTime(birthday); u.Nation = nation; bool ok = new UsersData().Insert(u); Session["InsertOK"] = ok; return RedirectToAction("Index", "Home"); } public ActionResult Delete(string id) { bool ok = new UsersData().DeleteUser(id); return RedirectToAction("Index"); } public ActionResult Update(string id) { Users u = new UsersData().SelectUser(id); ViewBag.hehe = u; return View(); } }}
Models层:
using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace mvc2.Models{ public class UsersData { DataClasses1DataContext con = new DataClasses1DataContext(); public ListSelectAll() { return con.Users.ToList(); } public bool Insert(Users u) { bool ok = false; try { con.Users.InsertOnSubmit(u); con.SubmitChanges(); ok = true; } catch { } return ok; } public bool DeleteUser(string ids) { bool ok = false; Users u = con.Users.Where(r => r.Ids.ToString() == ids).FirstOrDefault(); if (u != null) { con.Users.DeleteOnSubmit(u); con.SubmitChanges(); ok = true; } return ok; } public Users SelectUser(string ids) { return con.Users.Where(r => r.Ids.ToString() == ids).FirstOrDefault(); } }}
views层:
index:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %><%@ Import Namespace="mvc2.Models" %> Index <% if (Convert.ToBoolean(Session["InsertOK"]) == true) { %> <% Session["InsertOK"] = null; } %>ulist = new UsersData().SelectAll(); foreach (Users u in ulist) { %>
<% List 用户名 密码 昵称 性别 生日 民族 操作 <% } %> <%=u.UserName %> <%=u.PassWord %> <%=u.NickName %>同学 <%=u.Sex.Value?"男":"女" %> <%=u.Birthday.Value.ToString("yyyy年MM月dd日") %> <%=u.UserNation.NationName %> 修改 | 删除
insert:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %> Insert 添加用户
update:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %><%@ Import Namespace="mvc2.Models" %> Update 修改用户
<% Users u = ViewBag.hehe; %>