解释在MVC控制器的代码? [英] explain the code of controller in mvc?

查看:130
本文介绍了解释在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);
}
}
}

推荐答案

使用HttpGet和HttpPost进行简单的 CRUD 操作.
HttpGet方法用于查看详细信息,而HttpPost方法用于提交数据.
而且方法名称非常清晰,因此我不必解释每个方法的用途.

实体框架和Linq用于查询数据.
您可以在这里找到有关它们的更多详细信息,
https://msdn.microsoft.com/en-in/data/ef.aspx [ ^ ]
https://msdn.microsoft.com/en-us/library/bb397926.aspx [ ^ ]

希望这对您来说很清楚,然后调试每一行,看看代码实际发生了什么:)

-KR
Simple CRUD operations with HttpGet and HttpPost.
HttpGet methods are to view the details, and HttpPost are to submit the data.
And method names are pretty much clear so I don''t have to explain what each is for.

Entity Framework and Linq is used to query the data.
You can find more details about them here,
https://msdn.microsoft.com/en-in/data/ef.aspx[^]
https://msdn.microsoft.com/en-us/library/bb397926.aspx[^]

Hope this much is clear to you and not, then debug each line and see what is actually happening with the code :)

-KR


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆