mvc控制层Controllers增删改查方法

一、添加
[HttpPost]
public ActionResult Create(City city)
{
    if (ModelState.IsValid)
    {
        db.Citys.Add(city);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    //添加代码
    if (Request.IsAjaxRequest())
    {
         return PartialView(city);
    }
    return View(city);
}

public ActionResult Insert()
{
    var c = new City
    {
        Id = 123,
        Name = "刘德华",
        Tel = "1236545"
    };
    db.Citys.Add(c);
    db.SaveChanges();
    return RedirectToAction("Index");
}

二、修改
public ActionResult Edit(int id = 0)
{
    City city = db.Citys.Find(id);
    if (city == null)
    {
        return HttpNotFound();
    }
    return View(city);
}

[HttpPost]
public ActionResult Edit(City city)
{
    if (ModelState.IsValid)
    {
        db.Entry(city).State = EntityState.Modified;
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(city);
}

/// <summary>
/// 修改全部
/// </summary>
/// <param name="collection"></param>
public ActionResult Update_Node(FormCollection collection)
{
    var query = db.Nodes.ToList();
    foreach (Node m in query)
    {
        m.IsShow = "Flase";
    }
    db.SaveChanges();
    return RedirectToAction("Index");
}

/// <summary>
/// 批量修改
/// </summary>
/// <param name="collection"></param>
public ActionResult Update_Node(FormCollection collection)
{
    string chkValues = Request.Form["item"];
    if(chkValues != null)
    {
        string[] strtemp = chkValues.Split(',');
        foreach (string strs in strtemp)
        {
            int nodeID = Convert.ToInt32(strs.ToString());
            Node m = this.db.Nodes.Find(nodeID);
            m.NodeID = nodeID;
            m.IsShow = "True";
        }
        db.SaveChanges();
        return RedirectToAction("Index");
    }
}

三、删除
public ActionResult Delete(int id = 0)
{
    City city = db.Citys.Find(id);
    if (city == null)
    {
        return HttpNotFound();
    }
    return View(city);
}

[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
    City city = db.Citys.Find(id);
    db.Citys.Remove(city);
    db.SaveChanges();
    return RedirectToAction("Index");
}

四、查询
1、方法
[HttpPost]
public JsonResult Auto_Read(string term)
{
    //需要调用下面的方法“GetPublishmentSystemsList”
    return Json(GetPublishmentSystemsList(term), JsonRequestBehavior.AllowGet);
}

/// <summary>
/// 查询列表
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
private IEnumerable<PublishmentSystem> GetPublishmentSystemsList(string name)
{
    var query = from p in db.PublishmentSystems
                where p.PublishmentSystemName.Contains(name)
                select new
                {
                    Id = p.PublishmentSystemID,
                    Name = p.PublishmentSystemName,
                };

    return query.ToList().Select(C => new PublishmentSystem
    {
        PublishmentSystemID = C.Id,
        PublishmentSystemName = C.Name,
    });
}

2、方法
public ActionResult GetData(string term)
{
    var data = GetPublishmentSystem(term);
    return Json(data, JsonRequestBehavior.AllowGet);
}

public object GetPublishmentSystem(string term)
{
    return this.db.PublishmentSystems
           .Where(p => p.PublishmentSystemName.Contains(term))
           .OrderBy(p => p.PublishmentSystemName)
           .Select(p => new
           {
               value = p.PublishmentSystemID,
               label = p.PublishmentSystemName
           }).ToArray();
}
===============================================================

跳转方法
1、方法
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult LogOff()
{
    WebSecurity.Logout();
    return RedirectToAction("Index", "Home");
}

2、方法
return RedirectToAction("Manage");

3、方法
return RedirectToAction("Manage", new { Message = message });

4、方法
ModelState.AddModelError("", "当前密码不正确或新密码无效。");

你可能感兴趣的:(mvc控制层Controllers增删改查方法)