zoukankan      html  css  js  c++  java
  • MVC数据修改

    1、视图(显示)界面代码:

    @{
        Layout = null;
    }
    @using Mvc1.Models;
    
    <!DOCTYPE html>
    
    @{
        if(Session["user"]==null)
        {
            Response.Redirect("Login");
        }
        
        }
    
    
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Index</title>
    </head>
    <body>
        <div>
            <table style="background-color: navy; text-align: center;  100%;">
                <tr style="color: white;">
                    <td>用户名</td>
                    <td>密码</td>
                    <td>昵称</td>
                    <td>性别</td>
                    <td>生日</td>
                    <td>民族</td>
                    <td>操作</td>
                </tr>
    
                @{
                    //先将数据取出来
                    List<Users> list = new UsersData().Select();
    
                    foreach (Users u in list)
                    {
                    <tr style="background-color: #e0e0e0;">
                        <td>@u.UserName</td>
                        <td>@u.PassWord</td>
                        <td>@u.NickName</td>
                        <td>@((bool)u.Sex ? "" : "")</td>
                        <td>@Convert.ToDateTime(u.Birthday).ToString("yyyy年MM月dd日")</td>
                        <td>@u.NationName</td>
                        <td>
                            <a href="Home/Update/@u.UserName">修改</a>
                        </td>
                    </tr>
                    }
                }
            </table>
        </div>
    </body>
    </html>

    2、视图(修改)界面代码:

    @{
        Layout = null;
    }
    @using Mvc1.Models;
    @model Users
    
    <!DOCTYPE html>
    
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Update</title>
    </head>
    <body>
        <form name="form1" method="post" action="~/Home/UUU">
            用户名:<input type="text" name="username" value="@Model.UserName" /><br />
            密码:<input type="text" name="password" value="@Model.PassWord" /><br />
            昵称:<input type="text" name="nickname" value="@Model.NickName" /><br />
            性别:
                    @{
                        if ((bool)Model.Sex)
                        {
                        <input type="radio" name="sex" checked="checked" value="True" />@:男
                        <input type="radio" name="sex" value="False" />@:女
                        }
                        else
                        {
                        <input type="radio" name="sex" value="True" />@:男
                        <input type="radio" name="sex" checked="checked" value="False" />@:女
                        }
                    }
            <br />
            生日:
            <select id="year" onchange="DateChange()">
                @{
                    for (int i = DateTime.Now.Year; i > 1900; i--)
                    {
                        if (Convert.ToDateTime(Model.Birthday).Year == i)
                        { 
                    <option selected="selected">@i</option>
                        }
                        else
                        {
                    <option>@i</option>
                        }
                    }
                }
            </select><select id="month" onchange="DateChange()">
                @{
                    for (int i = 1; i <= 12; i++)
                    {
                        if (Convert.ToDateTime(Model.Birthday).Month == i)
                        {
                    <option selected="selected">@i</option>
                        }
                        else
                        {
                    <option>@i</option>
                        }
                    }
                }
            </select><select id="day" onchange="DateChange()">
                @{
                    for (int i = 1; i <= 31; i++)
                    {
                        if (Convert.ToDateTime(Model.Birthday).Day == i)
                        {
                    <option selected="selected">@i</option>
                        }
                        else
                        {
                    <option>@i</option>
                        }
                    }
                }
            </select><input type="text" id="birthday" name="birthday" style="display:none;" value="@Model.Birthday" />
    
            <br />
            民族:
            <select name="nation">
                @{
                    List<Nation> Nlist = new NationData().Select();
    
                    foreach (var n in Nlist)
                    {
                        if (Model.Nation == n.NationCode)
                        {
                    <option value="@n.NationCode" selected="selected">@n.NationName</option>
                        }
                        else
                        {
                    <option value="@n.NationCode">@n.NationName</option>
                        }
                    }
                }
            </select>
    
            <br />
            <input type="button" value="提交" onclick="form1.submit()" />
        </form>
    </body>
    </html>
    <script type="text/javascript">
        function DateChange() {
            var year = document.getElementById('year').value;
            var month = document.getElementById('month').value;
            var day = document.getElementById('day').value;
            document.getElementById('birthday').value = year + "-" + month + "-" + day;
        }
    </script>

    3、控制层控制器代码:

    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
        {
            public ActionResult Index()
            {
                return View();
            }
    
            public ActionResult Update(string id)
            {
                Users u = new UsersData().Select(id);
    
                return View(u);
            }
    
            public ActionResult UUU(Users data)
            {
                new UsersData().Update(data);
    
                return RedirectToAction("Index", "Home");
            }
        }
    }

    4、模型层代码:

    民族表查询

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace Mvc1.Models
    {
        public class NationData
        {
            Data0425DataContext con = new Data0425DataContext();
            public List<Nation> Select()
            {
                return con.Nation.ToList();
            
            }
    
        }
    }

    民族字段扩展

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace Mvc1.Models
    {
        public partial class Users
        {
            Data0425DataContext con = new Data0425DataContext();
    
            public string NationName
            {
                get {
                    return con.Nation.Where(r => r.NationCode == this.Nation).FirstOrDefault().NationName;
                }
            }
    
        }
    }

    数据操作方法

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace Mvc1.Models
    {
        public class UsersData
        {
            Data0425DataContext con = new Data0425DataContext();
    
            public List<Users> Select()
            {
                return con.Users.ToList();
            }
    
            public Users Select(string uname)
            {
                return con.Users.Where(r => r.UserName == uname).FirstOrDefault();
            }
    
            public void Update(Users uuu)
            {
                Users u = con.Users.Where(r => r.UserName == uuu.UserName).FirstOrDefault();
    
                u.PassWord = uuu.PassWord;
                u.NickName = uuu.NickName;
                u.Sex = uuu.Sex;
                u.Birthday = uuu.Birthday;
                u.Nation = uuu.Nation;
    
                con.SubmitChanges();
            }
        }
    }
  • 相关阅读:
    详解学习C#的方法和步骤
    将今天写进历史,即可得出现在的世界是数字的
    60秒,我们可以干什么?
    《每个人都会死,但我总以为自己不会》让我直面死亡
    介绍Ext JS 4.2的新特性的《深入浅出Ext JS》上市
    十一阅读攻略:和土豪做朋友,告别穷屌丝,迎接高富帅,成功逆袭!
    超低成本----音视频通讯+共享屏幕+电子白板
    Hbase split的三种方式和split的过程
    分布式数据库 HBase
    Sqoop-1.4.6.bin__hadoop-2.0.4-alpha 环境搭建
  • 原文地址:https://www.cnblogs.com/123lucy/p/5828655.html
Copyright © 2011-2022 走看看