• MVC ---- 怎删改查


    using Modelsop;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using Newtonsoft.Json;
    using System.Data.Entity.Infrastructure;
    using System.Data.Entity;
    
    namespace WebApplication1.Controllers
    {
        public class HomeController : Controller
        {
    
            SuperCodeEntities context = new SuperCodeEntities();
    
            public ActionResult Index()
            {
              
                return View();
            }
    
            public ActionResult About()
            {
                ViewBag.Message = "Your application description page.";
    
                return View();
            }
    
            public ActionResult Contact()
            {
                ViewBag.Message = "Your contact page.";
    
                return View();
            }
    
    
            public ActionResult Add(Test user)
            {
    
                string reslt = null;
                int reslut = 0;
                try
                {
                   context.Test.Add(user);
                   reslut= context.SaveChanges();
    
                   if (reslut > 0)
                   {
                       reslt = "添加成功!";
                   }
                   else
                   {
                       reslt = "添加失败!";
                   } 
                }
                catch (Exception)
                {
    
                    throw;
                }
    
                return Content(JsonConvert.SerializeObject(reslt));
            }
    
    
            public ActionResult TestList() {
                List<Test> tt = context.Set<Test>().ToList();
    
                return Content(JsonConvert.SerializeObject(tt));
            }
    
            public ActionResult Del(int Id)
            {
    
                string reslt = null;
                int reslut = 0;
    
                Test tt = context.Test.Where(m => m.ID == Id).FirstOrDefault();
    
                context.Test.Attach(tt);
                context.Test.Remove(tt);
                reslut = context.SaveChanges();
                if (reslut > 0)
                {
                    reslt = "添加成功!";
                }
                else
                {
                    reslt = "添加失败!";
                }
    
                return Content(JsonConvert.SerializeObject(reslt));
            }
    
            public ActionResult Update(Test tt) {
    
                string reslt = null;
                int reslut = 0;
    
                var ts = context.Test.Find(tt.ID);
                ts.Name = tt.Name;
                ts.Msisdn = tt.Msisdn;
                context.Entry<Test>(ts).State = EntityState.Modified;
                reslut = context.SaveChanges();
                if (reslut > 0)
                {
                    reslt = "添加成功!";
                }
                else
                {
                    reslt = "添加失败!";
                }
    
                return Content(JsonConvert.SerializeObject(reslt));
    
            }
    
            public ActionResult eidt(int id)
            {
                Test tt = context.Test.Where(m => m.ID == id).FirstOrDefault();
    
                return View(tt);
            }
    
        }
    }
  • 相关阅读:
    Highmaps网页图表教程之图表配置项结构与商业授权
    架构要素--安全性
    Select * 一定不走索引是否正确?
    uvalive 6669 hidden tree(好壮压dp)
    KVM硬件辅助虚拟化之 EPT in Nested Virtualization
    HDU 1043 八数码(A*搜索)
    在夕阳再晨的日子里(二)----掌管市场部的岁月之合作团队与社区的拓展
    Android开发数据库之第三方ORM框架(GreenDao)
    【MySQL】常见错误与经常使用命令的集锦
    Service-监听手机来电
  • 原文地址:https://www.cnblogs.com/youmingkuang/p/9557550.html
Copyright © 2020-2023  润新知