Mvc增删改查

174 阅读1分钟

using System; using System.Collections.Generic; using System.Data; using System.Data.Entity; using System.Linq; using System.Net; using System.Web; using System.Web.Mvc; using WebApplication1.Models;

namespace WebApplication1.Controllers { public class aController : Controller { private Model1 db = new Model1();

    // GET: a
    public ActionResult Index(string AssetName, string AssetTypeID)
    {
       //显示
        var b = db.AssetInfo.Select(x => new Class1()
        {
            AssetID = x.AssetID,
            AssetName = x.AssetName,
            AssetTypeID = x.AssetTypeID,
            AssetModel = x.AssetModel,
            AssetCompany = x.AssetCompany,
            AssetReMark = x.AssetReMark,
            Names = x.AssetTypeInfo.TypeName
        }).ToList();

      //查询
        ViewBag.AssetTypeID = List();
        ViewBag.name = AssetName;
        if (!string.IsNullOrEmpty(AssetName))
        {
            b = b.Where(x => x.AssetName == AssetName).ToList();
        }
        if (!string.IsNullOrEmpty(AssetTypeID))
        {
            b = b.Where(x => x.AssetTypeID ==Convert.ToInt32(AssetName)).ToList();
        }
        return View(b.ToList());
    }
     //下拉框
    public List<SelectListItem> List() {
        var a = db.AssetTypeInfo.ToList().Select(x => new SelectListItem()
        {
            Text = x.TypeName,
            Value = x.TypeID.ToString()
        }).ToList();
        return a;
    }


    // GET: a/Details/5
    public ActionResult Details(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        AssetInfo assetInfo = db.AssetInfo.Find(id);
        if (assetInfo == null)
        {
            return HttpNotFound();
        }
        return View(assetInfo);
    }
      //添加
    // GET: a/Create
    public ActionResult Create()
    {
        ViewBag.AssetTypeID = List();
        return View();
    }

    // POST: a/Create

    [HttpPost]
   
    public ActionResult Create( AssetInfo a)
    {
        ViewBag.AssetTypeID = List();
        if (ModelState.IsValid)
        {
            db.AssetInfo.Add(a);
            db.SaveChanges();
            ViewBag.info = "添加成功";
        }         
        return View(a);
    }
     //编辑
    // GET: a/Edit/5
    public ActionResult Edit(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        AssetInfo assetInfo = db.AssetInfo.Find(id);
        if (assetInfo == null)
        {
            return HttpNotFound();
        }
        ViewBag.AssetTypeID = new SelectList(db.AssetTypeInfo, "TypeID", "TypeName", assetInfo.AssetTypeID);
        return View(assetInfo);
    }

    // POST: a/Edit/5

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit([Bind(Include = "AssetID,AssetName,AssetTypeID,AssetModel,AssetCompany,AssetReMark")] AssetInfo assetInfo)
    {
        if (ModelState.IsValid)
        {
            db.Entry(assetInfo).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        ViewBag.AssetTypeID = new SelectList(db.AssetTypeInfo, "TypeID", "TypeName", assetInfo.AssetTypeID);
        return View(assetInfo);
    }

   // 删除
     [HttpPost]
     public ActionResult Delete(int? id)
    {
        var a = db.AssetInfo.FirstOrDefault(x =>x.AssetID== id);
        if (a!= null)
        {
            db.AssetInfo.Remove(a);
           db.SaveChanges();
        }
       return Json(new {Flag=1,Msg="删除成功!"});
    }


    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            db.Dispose();
        }
        base.Dispose(disposing);
    }
}

}