将CRUD操作放在asp.net中的单个文件上

时间:2019-03-26 08:26:52

标签: asp.net-mvc

**你好 我正在开发使用ASP.NET API的应用程序,我创建了控制器实体和MVC控制器,这些视图在视图中生成了CRUD操作,如下所示(参见图片),文件中的每个操作如何将其全部放入一个文件中。

**  enter image description here 控制器代码:

    // GET: /Clients/
public ActionResult Index()
{
    return View(db.CLIENT.ToList());
}

// GET: /Clients/Details/5
public ActionResult Details(long? id)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    Client_H Client_H = db.Client_H.Find(id);
    if (Client_H == null)
    {
        return HttpNotFound();
    }
    return View(Client_H);
}

// GET: /Clients/Create
public ActionResult Create()
{
    return View();
}

// POST: /Clients/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for 
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include="ID,nom,prenom,CODE,ADRESSE,BQE_VILLE,ADRESSE,TEL")] Client_H Client_H)
{
    if (ModelState.IsValid)
    {
        db.Client_H.Add(Client_H);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(Client_H);
}

// GET: /Clients/Edit/5
public ActionResult Edit(long? id)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    Client_H Client_H = db.Client_H.Find(id);
    if (Client_H == null)
    {
        return HttpNotFound();
    }
    return View(Client_H);
}

// POST: /Clients/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for 
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include="ID,nom,prenom,CODE,ADRESSE,BQE_VILLE,ADRESSE,TEL")] Client_H Client_H)
{
    if (ModelState.IsValid)
    {
        db.Entry(Client_H).State = EntityState.Modified;
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(Client_H);
}

// GET: /Clients/Delete/5
public ActionResult Delete(long? id)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    Client_H Client_H = db.Client_H.Find(id);
    if (Client_H == null)
    {
        return HttpNotFound();
    }
    return View(Client_H);
}

// POST: /Clients/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(long id)
{
    Client_H Client_H = db.Client_H.Find(id);
    db.Client_H.Remove(Client_H);
    db.SaveChanges();
    return RedirectToAction("Index");
}

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

}

提前谢谢

1 个答案:

答案 0 :(得分:0)

我不太了解“如何将所有内容放在一个文件中”是什么意思?

如果要在1个视图中使用所有CRUD功能,则可以使用jquery POST&GET。