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);
        }

    }
}
复制代码

 

posted @   幽冥狂_七  阅读(214)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
历史上的今天:
2017-08-29 Python 递归
2017-08-29 Python lambda 表达式
2017-08-29 Python 函数
点击右上角即可分享
微信分享提示