using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVC20.Models;
using System.ComponentModel;
namespace MVC20.Controllers
{
public class CategoryController : Controller
{
// DefaultValueAttribute - 用于为参数指定默认值。当路由或url参数中无此参数时,则会使用此默认值
ActionResult Index() ActionResult Index([DefaultValue(2)]int pageIndex)
{
int pageSize = 10;
var categories =
new Models.CategorySystem().GetCategory(pageIndex, pageSize);
return View(
"Index", categories);
}
ActionResult Edit() ActionResult Edit(int id)
{
var category =
new Models.CategorySystem().GetCategory(id);
return View(
"Edit", category);
}
// 用于演示实现 Model 级别的 Dynamic Data
[HttpPost]
ActionResult Edit() ActionResult Edit(int id, FormCollection formValues)
{
var cs =
new Models.CategorySystem();
var category = cs.GetCategory(id);
TryUpdateModel<ProductCategory>(category);
cs.Save();
return RedirectToAction(
"Index");
}
ActionResult Edit2() ActionResult Edit2(int id)
{
var category =
new Models.CategorySystem().GetCategory(id);
return View(
"Edit2", category);
}
// 用于演示实现 Property 级别的 Dynamic Data
[HttpPost]
ActionResult Edit2() ActionResult Edit2(int id, FormCollection formValues)
{
var cs =
new Models.CategorySystem();
var category = cs.GetCategory(id);
TryUpdateModel<ProductCategory>(category);
cs.Save();
return RedirectToAction(
"Index");
}
ActionResult Details() ActionResult Details(int id)
{
var category =
new Models.CategorySystem().GetCategory(id);
return View(
"Details", category);
}
ActionResult Create() ActionResult Create()
{
ProductCategory category =
new ProductCategory()
{
};
return View(category);
}
// 用于演示通过 DataAnnotations 实现数据验证
[HttpPost]
ActionResult Create() ActionResult Create(ProductCategory category)
{
if (!ModelState.IsValid)
{
return View(category);
}
else {
var cs =
new Models.CategorySystem();
cs.AddCategory(category);
cs.Save();
return View(
"Details", category);
}
}
}
}