本文介绍了解释在MVC控制器的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以解释该控制器的功能和箭头标记代码行的用途吗
这是我的代码
使用系统;
使用System.Collections.Generic;
使用System.Data;
使用System.Data.Entity;
使用System.Linq;
使用System.Web;
使用System.Web.Mvc;
使用SampleAdodotNet.Models;

命名空间SampleAdodotNet.Controllers
{
公共类CategoryController:控制器
{
->私人hussainEntities db = new hussainEntities();

//
//GET:/类别/

公共ViewResult Index()
{
返回View(db.Categories.ToList());
}

//
//GET:/类别/详细信息/5

公共ViewResult详细信息(int id)
{
->类别category = db.Categories.Single(c => c.CategoryID == id);
return View(category);
}

//
//GET:/类别/创建

公共ActionResult Create()
{
返回View();
}

//
//POST:/Category/Create

[HttpPost]
公共ActionResult创建(类别类别)
{
如果(ModelState.IsValid)
{
db.Categories.AddObject(category);
db.SaveChanges();
返回RedirectToAction("Index");
}

return View(category);
}

//
//GET:/Category/Edit/5

公共ActionResult编辑(int id)
{
->类别category = db.Categories.Single(c => c.CategoryID == id);
return View(category);
}

//
//POST:/Category/Edit/5

[HttpPost]
公共ActionResult编辑(类别类别)
{
如果(ModelState.IsValid)
{
db.Categories.Attach(category);
db.ObjectStateManager.ChangeObjectState(category,EntityState.Modified);
db.SaveChanges();
返回RedirectToAction("Index");
}
return View(category);
}

//
//GET:/类别/删除/5

public ActionResult Delete(int id)
{
->类别category = db.Categories.Single(c => c.CategoryID == id);
return View(category);
}

//
//POST:/Category/Delete/5

[HttpPost,ActionName("Delete")]
公共ActionResult DeleteConfirmed(int id)
{
->类别category = db.Categories.Single(c => c.CategoryID == id);
db.Categories.DeleteObject(category);
db.SaveChanges();
返回RedirectToAction("Index");
}

受保护的重写void Dispose(布尔处置)
{
db.Dispose();
base.Dispose(处置);
}
}
}

can any one explain what is the function and what is use of arrow marked lines of code in this controller
here is my code
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SampleAdodotNet.Models;

namespace SampleAdodotNet.Controllers
{
public class CategoryController : Controller
{
--> private hussainEntities db = new hussainEntities();

//
// GET: /Category/

public ViewResult Index()
{
return View(db.Categories.ToList());
}

//
// GET: /Category/Details/5

public ViewResult Details(int id)
{
--> Category category = db.Categories.Single(c => c.CategoryID == id);
return View(category);
}

//
// GET: /Category/Create

public ActionResult Create()
{
return View();
}

//
// POST: /Category/Create

[HttpPost]
public ActionResult Create(Category category)
{
if (ModelState.IsValid)
{
db.Categories.AddObject(category);
db.SaveChanges();
return RedirectToAction("Index");
}

return View(category);
}

//
// GET: /Category/Edit/5

public ActionResult Edit(int id)
{
--> Category category = db.Categories.Single(c => c.CategoryID == id);
return View(category);
}

//
// POST: /Category/Edit/5

[HttpPost]
public ActionResult Edit(Category category)
{
if (ModelState.IsValid)
{
db.Categories.Attach(category);
db.ObjectStateManager.ChangeObjectState(category, EntityState.Modified);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(category);
}

//
// GET: /Category/Delete/5

public ActionResult Delete(int id)
{
--> Category category = db.Categories.Single(c => c.CategoryID == id);
return View(category);
}

//
// POST: /Category/Delete/5

[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
--> Category category = db.Categories.Single(c => c.CategoryID == id);
db.Categories.DeleteObject(category);
db.SaveChanges();
return RedirectToAction("Index");
}

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

推荐答案


这篇关于解释在MVC控制器的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 21:55