首页 > 代码库 > MVC

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="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=http://www.mamicode.com/"/home/delete?key=<%=u.ids %>">删除</a>
                     <a href=http://www.mamicode.com/"home/update/<%=u.username %>">修改</a>
                 </td>
            </tr>
            <%} %>
        </table>
        <a href=http://www.mamicode.com/"/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=http://www.mamicode.com/"添加" />
        </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=http://www.mamicode.com/"<%=uu.username %>"/><br />
            密码:<input type="password" name="password" value=http://www.mamicode.com/"<%=uu.password %>" /><br />
            昵称:<input type="text" name="nickname" value=http://www.mamicode.com/"<%=uu.nickname %>" /><br />
            性别:<input type="text" name="sex"  value=http://www.mamicode.com/"<%=uu.sex %>"/><br />
            生日:<input type="text" name="birthday" value=http://www.mamicode.com/"<%=uu.birthday %>" /><br />
            民族:<input type="text" name="nation" value=http://www.mamicode.com/"<%=uu.nation %>" /><br />
            班级:<input type="text" name="class" value=http://www.mamicode.com/"<%=uu.@class %>" /><br />
            <input type="submit" value=http://www.mamicode.com/"修改" />
        </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=http://www.mamicode.com/"/home/update/@u.username">修改</a>
                 </td>
            </tr>
            }
        </table>
        <a href=http://www.mamicode.com/"/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=http://www.mamicode.com/"true" />男<input type="radio" name="sex" value=http://www.mamicode.com/"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=http://www.mamicode.com/"@n.nationcode">@n.nationname</option>
            }
            }
        </select><br />
        班级:<input type="text" name="cla" /><br />
        <input type="submit" value=http://www.mamicode.com/"添加" />
    </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=http://www.mamicode.com/"@u.username" readonly="readonly"/><br />
            @:密码:<input type="password" name="password" value=http://www.mamicode.com/"@u.password" /><br />
            @:昵称:<input type="text" name="nickname" value=http://www.mamicode.com/"@u.nickname" /><br />
            @:性别:<input type="radio" name="sex" value=http://www.mamicode.com/"true" @(u.sex.Value ? "checked=\"checked\"" : "") />男<input type="radio" name="sex" value=http://www.mamicode.com/"false" @(u.sex.Value ? "" : "checked=\"checked\"") />女<br />
            @:生日:<input type="text" name="birthday" value=http://www.mamicode.com/"@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=http://www.mamicode.com/"@n.nationcode">@n.nationname</option>
                    }
                }
            </select><br />
            @:班级:<input type="text" name="Class" value=http://www.mamicode.com/"@u.Class" /><br />
           
            <input type="submit" value=http://www.mamicode.com/"确认修改" />
        }
    </form>
</body>
</html>

 

MVC