码迷,mamicode.com
首页 > Web开发 > 详细

MVC

时间:2017-03-06 21:14:56      阅读:459      评论:0      收藏:0      [点我收藏+]

标签:direct   用户   ext   redirect   conf   密码   cte   post   删除   

html代码与c#代码混合使用

aspx引擎:

模型代码:

创建linq

userdata.cs

namespace MvcApplication1.Models
{
    public class userdata
    {
        data0928DataContext con = new data0928DataContext();

        public List<users> select()
        {
            return con.users.ToList();
        }
    }
}

 

控制器代码:

namespace MvcApplication1.Controllers
{
    public class homeController : Controller
    {
        //
        // GET: /home/

        public ActionResult Index()
        {
            if (Request.Cookies["user"] != null)
                return View();
            else
                return RedirectToAction("login","ulogin");
        }
        public ActionResult insert()
        {
            return View();
        }
        public ActionResult insert1(string username,string password,string nickname,string sex,string birthday,string nation,string cla)
        {
            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;
            u.@class = cla;
            using (data0928DataContext con = new data0928DataContext())
            {
                con.users.InsertOnSubmit(u);
                con.SubmitChanges();
            }
            return RedirectToAction("index","home");
        }


        public ActionResult delete()
        {
            int s = Convert.ToInt32(Request["key"]);
            using (data0928DataContext con = new data0928DataContext())
            {
                users u = con.users.Where(r=>r.ids==s).FirstOrDefault();
                if (u != null)
                {
                    con.users.DeleteOnSubmit(u);
                    con.SubmitChanges();
                }
            }

            return RedirectToAction("index","home");
        }


        public ActionResult update(string id)
        {
            users u = null;
            using (data0928DataContext con = new data0928DataContext())
            {
                u = con.users.Where(r=>r.username==id).FirstOrDefault();
            }
            ViewBag.ff = u;
            return View();
        }


        public ActionResult update1(users uu)
        {
            using (data0928DataContext con = new data0928DataContext())
            {
                users u = con.users.Where(r=>r.username==uu.username).FirstOrDefault();
                if (u != null)
                {
                    u.password = uu.password;
                    u.nickname = uu.nickname;
                    u.sex = uu.sex;
                    u.birthday = uu.birthday;
                    u.nation = uu.nation;
                    u.@class = uu.@class;
                    con.SubmitChanges();
                }
            }

            return RedirectToAction("index","home");
        }

    }
}

视图代码:
index.aspx

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<%@ Import Namespace="MvcApplication1.Models" %>
<!DOCTYPE html>

<html>
<head runat="server">
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div>
        
        <table style="width:100%;background-color:navy;color:white;text-align:center;">

            <tr>
                <td>用户名</td>
                <td>密码</td>
                <td>昵称</td>
                <td>性别</td>
                <td>生日</td>
                <td>民族</td>
                <td>班级</td>
                <td>操作</td>
            </tr>
            <%
                
                List<users> ulist = new userdata().select();
                foreach(users u in ulist )
                {
                 %>

             <tr style="background-color:gray;">
                <td><%=u.username %></td>
                <td><%=u.password %></td>
                <td><%=u.nickname %></td>
                <td><%=u.sex %></td>
                <td><%=u.birthday %></td>
                <td><%=u.nation1.nationname %></td>
                <td><%=u.class1.classname %></td>
                 <td>
                     <a onclick="return confirm(‘确定删除吗?‘)" href="/home/delete?key=<%=u.ids %>">删除</a>
                     <a href="home/update/<%=u.username %>">修改</a>
                 </td>
            </tr>
            <%} %>
        </table>
        <a href="/home/insert">添加</a>

    </div>
</body>
</html>

insert.aspx

<%@ 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>
        <form action="/home/insert1" method="post">
            <h1>用户添加</h1>
            用户名:<input type="text" name="username" /><br />
            密码:<input type="password" 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="text" name="cla" /><br />
            <input type="submit" value="添加" />
        </form>
    </div>
</body>
</html>

update.aspx

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<%@ Import Namespace="MvcApplication1.Models" %>
<!DOCTYPE html>

<html>
<head runat="server">
    <meta name="viewport" content="width=device-width" />
    <title>update</title>
</head>
<body>
    <div>
        <form action="/home/update1" method="post">

            <%
                users uu = ViewBag.ff as users;
                 %>

            <h1>用户修改</h1>
            用户名:<input type="text" name="username" readonly="readonly" value="<%=uu.username %>"/><br />
            密码:<input type="password" name="password" value="<%=uu.password %>" /><br />
            昵称:<input type="text" name="nickname" value="<%=uu.nickname %>" /><br />
            性别:<input type="text" name="sex"  value="<%=uu.sex %>"/><br />
            生日:<input type="text" name="birthday" value="<%=uu.birthday %>" /><br />
            民族:<input type="text" name="nation" value="<%=uu.nation %>" /><br />
            班级:<input type="text" name="class" value="<%=uu.@class %>" /><br />
            <input type="submit" value="修改" />
        </form>
    </div>
</body>
</html>


razor引擎:

模型代码:

创建linq

userdata.cs

namespace razor.Models
{
    public class userdata
    {
        data0928DataContext con = new data0928DataContext();
        public List<users> select()
        {
            return con.users.ToList();
        }
    }
}

控制器代码:

namespace razor.Controllers
{
    public class homeController : Controller
    {
        //
        // GET: /home/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult update(string id)
        {
            List<nation> nlist = null;
            
            users u = null;
            using (data0928DataContext con = new data0928DataContext())
            {
                u = con.users.Where(r=>r.username==id).FirstOrDefault();
                nlist = con.nation.ToList();
                
            }
            ViewBag.st = u;
            ViewBag.sr = nlist;
           

            return View();
        }

        public ActionResult update1(users uu)
        {
            using (data0928DataContext con = new data0928DataContext())
            {
                users u = con.users.Where(r => r.username == uu.username).FirstOrDefault();
                u.password = uu.password;
                u.nickname = uu.nickname;
                u.sex = uu.sex;
                u.birthday = uu.birthday;
                u.nation = uu.nation;
                u.Class = uu.Class;
                con.SubmitChanges();
            }
            return RedirectToAction("index","home");
        }

        public ActionResult insert()
        {
            List<nation> nlist = null;
            using (data0928DataContext con = new data0928DataContext())
            {
                nlist = con.nation.ToList();
            }
            ViewBag.ss = nlist;

            return View();
        }
        public ActionResult insert1(string username,string password,string nickname,string sex,string birthday,string nation,string cla)
        {
            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;
            u.Class = cla;
            using (data0928DataContext con = new data0928DataContext())
            {
                con.users.InsertOnSubmit(u);
                con.SubmitChanges();
            }
            return RedirectToAction("index","home");
        
        }


    }
}

界面代码:
index.cshtml

@{
    Layout = null;
}
@using razor.Models;

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div>
        @{ List<users> ulist = new userdata().select();
            }
            
        <table style="width:100%;background-color:navy;color:white;text-align:center;">
            <tr>
                <td>用户名</td>
                <td>密码</td>
                <td>昵称</td>
                <td>性别</td>
                <td>生日</td>
                <td>民族</td>
                <td>班级</td>
                <td>操作</td>
            </tr>
            @foreach(users u in ulist )
            {
             <tr style="background-color:gray;">
                <td>@u.username</td>
                <td>@u.password</td>
                <td>@u.nickname</td>
                <td>@(u.sex.Value?"":"")</td>
                <td>@u.birthday</td>
                <td>@u.nation1.nationname</td>
                <td>@u.class1.classname</td>
                 <td>
                     <a href="/home/update/@u.username">修改</a>
                 </td>
            </tr>
            }
        </table>
        <a href="/home/insert" >添加</a>
        


        
    </div>
</body>
</html>

insert.cshtml

@{
    Layout = null;
}
@using razor.Models;

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>insert</title>
</head>
<body>
    <form action="/home/insert1" method="post">

        <h1>添加用户</h1>
        用户名:<input type="text" name="username" /><br />
        密码:<input type="password" name="password" /><br />
        昵称;<input type="text" name="nickname" /><br />
        性别:<input type="radio" name="sex" value="true" />男<input type="radio" name="sex" value="false" />女<br />
        生日:<input type="text" name="birthday" /><br />
        民族:
        <select name="nation">
            @{
                List<nation> nlist=ViewBag.ss as List<nation>;
                foreach(nation n in nlist)
                {
            <option value="@n.nationcode">@n.nationname</option>
            }
            }
        </select><br />
        班级:<input type="text" name="cla" /><br />
        <input type="submit" value="添加" />
    </form>
</body>
</html>

update.cshtml

@{
    Layout = null;
}
@using razor.Models;
<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>update</title>
</head>
<body>
    <form action="/home/update1" method="post">
        @{users u = ViewBag.st as users;

            <h1>用户修改</h1>
            @:用户名:<input type="text" name="username" value="@u.username" readonly="readonly"/><br />
            @:密码:<input type="password" name="password" value="@u.password" /><br />
            @:昵称:<input type="text" name="nickname" value="@u.nickname" /><br />
            @:性别:<input type="radio" name="sex" value="true" @(u.sex.Value ? "checked=\"checked\"" : "") />男<input type="radio" name="sex" value="false" @(u.sex.Value ? "" : "checked=\"checked\"") />女<br />
            @:生日:<input type="text" name="birthday" value="@u.birthday" /><br />
            @:民族:
            <select name="nation">
                @{
                    List<nation> nlist = ViewBag.sr as List<nation>;
                    foreach (nation n in nlist)
                    {
                    <option @(n.nationcode==u.nation?"selected=\"selected\"":"")  value="@n.nationcode">@n.nationname</option>
                    }
                }
            </select><br />
            @:班级:<input type="text" name="Class" value="@u.Class" /><br />
           
            <input type="submit" value="确认修改" />
        }
    </form>
</body>
</html>

 

MVC

标签:direct   用户   ext   redirect   conf   密码   cte   post   删除   

原文地址:http://www.cnblogs.com/wy1992/p/6511710.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!