• 2017-7-28 MVC 项目一


    项目一   :在界面输出  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 "<html><head></head><body><h1>这里是Test1,呵呵</h1></body></html>";
            }
    
            public string test2()
            {
                string s = "";
                List<Users> 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
        {
            /// <summary>
            /// 返回全部Users表数据
            /// </summary>
            /// <returns></returns>
            public List<Users> 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<Users> 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<dynamic>" %>
    
    <%@ Import Namespace="mvc2.Models" %>
    
    <!DOCTYPE html>
    
    <html>
    <head runat="server">
        <meta name="viewport" content="width=device-width" />
        <title>Index</title>
    </head>
    <body>
        <div>
            <% if (Convert.ToBoolean(Session["InsertOK"]) == true)
               {%>
    
            <script type="text/javascript">
                alert('添加成功!');
    
            </script>
            <%
                   Session["InsertOK"] = null;
               } %>
    
            <table style=" 100%; text-align: center; background-color: navy;">
                <tr style="color: white;">
                    <td>用户名</td>
                    <td>密码</td>
                    <td>昵称</td>
                    <td>性别</td>
                    <td>生日</td>
                    <td>民族</td>
                    <td>操作</td>
                </tr>
                <%
                    List<Users> ulist = new UsersData().SelectAll();
    
                    foreach (Users u in ulist)
                    {
                %>
                <tr style="background-color: white;">
                    <td><%=u.UserName %></td>
                    <td><%=u.PassWord %></td>
                    <td><%=u.NickName %>同学</td>
                    <td><%=u.Sex.Value?"":"" %></td>
                    <td><%=u.Birthday.Value.ToString("yyyy年MM月dd日") %></td>
                    <td><%=u.UserNation.NationName %></td>
                    <td>
                        <a href="Home/Update/<%=u.Ids %>">修改</a> | 
                        <a href="Home/Delete/<%=u.Ids %>">删除</a>
    
                    </td>
                </tr>
                <%
                    }
                %>
            </table>
            <input type="button" value="添加" onclick="window.open('Home/Insert', '_blank');" />
    
        </div>
    </body>
    </html>

    insert:

    <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
    
    <!DOCTYPE html>
    
    <html>
    <head runat="server">
        <meta name="viewport" content="width=device-width" />
        <title>Insert</title>
    </head>
    <body>
        <div>
            <h1>添加用户</h1>
    
            <form action="Insert1" method="post">
                用户名:
            <input type="text" name="username" /><br />
                密码:
            <input type="text" name="password" /><br />
                昵称:
            <input type="text" name="nickname" /><br />
                性别:
            <input type="text" name="sex" /><br />
                生日:
            <input type="text" name="birthday" /><br />
                民族:
            <input type="text" name="nation" /><br />
                <input type="submit" value="保存" />
            </form>
    
        </div>
    </body>
    </html>

    update:

    <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
    
    <%@ Import Namespace="mvc2.Models" %>
    
    <!DOCTYPE html>
    
    <html>
    <head runat="server">
        <meta name="viewport" content="width=device-width" />
        <title>Update</title>
    </head>
    <body>
        <h1>修改用户</h1>
    
        <%  Users u = ViewBag.hehe;  %>
        <form action="Update1" method="post">
            用户名:
            <input type="text" name="username" value="<%=u.UserName %>" /><br />
            密码:
            <input type="text" name="password" value="<%=u.PassWord %>" /><br />
            昵称:
            <input type="text" name="nickname" value="<%=u.NickName %>" /><br />
            性别:
            <input type="text" name="sex" value="<%=u.Sex %>" /><br />
            生日:
            <input type="text" name="birthday" value="<%=u.Birthday %>" /><br />
            民族:
            <input type="text" name="nation" value="<%=u.Nation %>" /><br />
            <input type="submit" value="保存" />
        </form>
    
    </body>
    </html>
  • 相关阅读:
    js实现去重字符串
    js查找水仙花数
    js实现找质因数
    jQuery插件(多级菜单)
    Pycharm安装常见问题
    Python-Excel循环写入
    1110 距离之和最小 V3
    1109 01组成的N的倍数
    1393 0和1相等串
    1043 幸运号码
  • 原文地址:https://www.cnblogs.com/zhengqian/p/7248431.html
Copyright © 2020-2023  润新知