zoukankan      html  css  js  c++  java
  • 新的方法(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>
  • 相关阅读:
    Message高级特性 & 内嵌Jetty实现文件服务器
    springboot中使用kindeditor富文本编辑器实现博客功能&vue-elementui使用vue-kindeditor
    Embarcadero RAD Studio XE5
    经典营销故事
    百度竞价教程 借助百度热力图让你的效果翻10倍
    无本借力:他是如何实现年收入70万?
    不用软件快速拥有几百个QQ群并都是管理员
    质保、保修、包修:含义不同
    域名反向解析在自建邮件群发服务器中的应用
    2014年1月1日,马年
  • 原文地址:https://www.cnblogs.com/wfaceboss/p/6490311.html
Copyright © 2011-2022 走看看