zoukankan      html  css  js  c++  java
  • MVC

    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="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="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>
  • 相关阅读:
    WPF 修改Webbrowser的IE版本小程序(32位)
    AnyCAD OpenSource 版本下载和编译
    请求ajax失败的原因(进入到error)
    如何将多个数据的- 转为:来匹配josn格式
    jQuery ajax如何传多个值到后台页面,举例:
    java finalize方法总结、GC执行finalize的过程
    SQL Server索引碎片整理实际操作记录
    MYSQL手册
    Eclipse显示行号
    MyEclipse设置Console输出到文件
  • 原文地址:https://www.cnblogs.com/wy1992/p/6511710.html
Copyright © 2011-2022 走看看