博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
2017-7-28 MVC 项目一
阅读量:7208 次
发布时间:2019-06-29

本文共 5656 字,大约阅读时间需要 18 分钟。

项目一   :在界面输出  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 = ""; List
ulist = 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 List
SelectAll() { 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; %>
用户名:
密码:
昵称:
性别:
生日:
民族:

 

转载于:https://www.cnblogs.com/zhengqian/p/7248431.html

你可能感兴趣的文章
块状链表 codevs 2333弹飞绵羊
查看>>
(九)jsMath
查看>>
CE_现金模组基本概念(概念)
查看>>
饭后一题
查看>>
Zynq-7000 FreeRTOS(一)系统移植配置
查看>>
[笔记][朝花夕拾][Multisim基础电路范例].第一章 RLC电路,第七、八节 米勒定理...
查看>>
免费论文查重
查看>>
【GPRS】GSM和GPRS模块的应用
查看>>
第一章 Docker简介和基本概念
查看>>
java代码-----实现打印三角形
查看>>
python(4) 小程序-异步加载
查看>>
20190327(练手感)
查看>>
modelform动态显示select标签的对象范围
查看>>
Android ---------- 富文本构建
查看>>
leetcode:Count Primes
查看>>
[转] babel的使用
查看>>
CentOS7.0安装与配置Tomcat-7
查看>>
C# SQL数据访问帮助类
查看>>
.net面试(汇总)
查看>>
.NET Entity Framework基本使用方法
查看>>