码迷,mamicode.com
首页 > Web开发 > 详细

MVC ---- 怎删改查

时间:2018-08-30 02:08:19      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:out   list()   eric   odi   upd   public   date   desc   add   

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

    }
}

 

MVC ---- 怎删改查

标签:out   list()   eric   odi   upd   public   date   desc   add   

原文地址:https://www.cnblogs.com/youmingkuang/p/9557550.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!