• 新的方法(Set<T>)实现mvc的crud


    model层的属性为:

     public partial class UserInfo
        {
            public int Uid { get; set; }
            public string UName { get; set; }
        }

    数据上下文类:生成数据

    using System;
        using System.Data.Entity;
        using System.Data.Entity.Infrastructure;
        
        public partial class MyContext : DbContext
        {
            public MyContext()
                : base("name=MyContext")
            {
            }
        
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                throw new UnintentionalCodeFirstException();
            }
        
            public DbSet<UserInfo> UserInfo { get; set; }
           
        }

    controller层:  crud是在集合的基础上完成的(实则对集合的crud)

     public class UserInfoCrudController : Controller
        {
            DbContext dbContext = new MyContext(); //由父类指向子类 实例化生成数据库 Set<>方法产生的是一个集合
            // GET: /UserInfoCrud/
    
            public ActionResult Index()
            {
                var list = dbContext.Set<UserInfo>();
    
                return View(list);
            }
    
            public ActionResult Add()
            {
                return View();
            }
            [HttpPost]
            public ActionResult Add(UserInfo userInfo)
            {
                dbContext.Set<UserInfo>().Add(userInfo);
                int result = dbContext.SaveChanges();//如果内存中的数据发生了变化,并且希望将这个变化映射到数据库,需要执行这个方法
                if (result > 0)
                {
                    return Redirect(@Url.Action("Index", "UserInfoCrud"));
                }
                else
                {
                    return Redirect(@Url.Action("Add"));
                }
            }
    
            public ActionResult Edit(int id)
            {
                ViewData.Model = dbContext.Set<UserInfo>()
                    .Where(u => u.Uid == id).FirstOrDefault();
    
                return View();
            }
            [HttpPost]
            public ActionResult Edit(UserInfo userInfo)
            {
                dbContext.Set<UserInfo>().AddOrUpdate(userInfo);
                int result = dbContext.SaveChanges();
                if (result > 0)
                {
                    return Redirect(Url.Action("Index"));
                }
                else
                {
                    return Redirect(Url.Action("Edit", new RouteValueDictionary(new
                    {
                        id = userInfo.Uid
                    })));
                }
    
            }
    
            public ActionResult Remove(int id)
            {
                var userInfo = dbContext.Set<UserInfo>()
                    .Where(u => u.Uid == id)
                    .FirstOrDefault();
                dbContext.Set<UserInfo>().Remove(userInfo);
                dbContext.SaveChanges();
    
                return Redirect(Url.Action("Index"));
            }
        }

    views层:

    (1)数据展示部分


    @using model所在 的命名空间 @model IQueryable
    <t2_EFTest.Models.UserInfo> @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Index</title> </head> <body> <div> @Html.ActionLink("添加","Add","UserInfoCrud") <hr/> <table border="1"> <tr> <th>编号</th> <th>姓名</th> <th>修改</th> <th>删除</th> </tr> @foreach (UserInfo userInfo in Model) { <tr> <td>@userInfo.Uid</td> <td>@userInfo.UName</td> <td> @Html.ActionLink("修改","Edit","UserInfoCrud", new RouteValueDictionary(new {id=@userInfo.Uid}),null) </td> <td> @Html.ActionLink("删除", "Remove", "UserInfoCrud", new RouteValueDictionary(new { id=@userInfo.Uid }),null) </td> </tr> } </table> </div> </body> </html>

    (2)数据添加部分

    @model t2_EFTest.Models.UserInfo
    @{
        Layout = null;
    }
    
    <!DOCTYPE html>
    
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Add</title>
    </head>
    <body>
        <div>
            @using (Html.BeginForm("Add", "UserInfoCrud", FormMethod.Post))
            {
                <span>姓名:</span>
                @Html.TextBoxFor(u=>u.UName)
                <br/>
                <input type="submit" value="添加"/>
            }
        </div>
    </body>
    </html>

    (3)数据修改部分

    @model t2_EFTest.Models.UserInfo
    @{
        Layout = null;
    }
    
    <!DOCTYPE html>
    
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Edit</title>
    </head>
    <body>
        <div>
            @using (Html.BeginForm("Edit", "UserInfoCrud", FormMethod.Post))
            {
                <span>编号:</span>
                @Model.Uid
                @Html.HiddenFor(u=>u.Uid)
                <br/>
                <span>姓名:</span>
                @Html.TextBoxFor(u=>u.UName)
                <br/>
                <input type="submit" value="修改"/>
            }
        </div>
    </body>
    </html>
  • 相关阅读:
    蛙蛙请教:问几个面向对象设计的问题
    推荐一些flash和asp.net结合开发的文章
    写一个跟踪的类库
    蛙蛙推荐:有关随机数的一些讨论
    蛙蛙推荐:用ASP.NET WEB Services和Flash MX 2004打造MP3播放器
    HiveQL学习
    Linux rpm 命令参数使用详解[介绍和应用]
    java.util.concurrent.locks.Condition 例子程序探讨
    [转]sudoers设置
    配置linux电脑
  • 原文地址:https://www.cnblogs.com/wfaceboss/p/6490311.html
Copyright © 2020-2023  润新知