80 lines
2.5 KiB
C#
80 lines
2.5 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Web;
|
|||
|
using System.Web.Mvc;
|
|||
|
using System.Data.Entity;
|
|||
|
using Weee.DAL;
|
|||
|
using Weee.Areas.Admin.ViewModels;
|
|||
|
using Weee.Models;
|
|||
|
using PagedList;
|
|||
|
|
|||
|
namespace Weee.Areas.Admin.Controllers
|
|||
|
{
|
|||
|
public class PCRController : AdminControllerBase
|
|||
|
{
|
|||
|
public PCRController(WeeeDataContext db) : base(db)
|
|||
|
{
|
|||
|
}
|
|||
|
|
|||
|
public ActionResult Index(int page = 1)
|
|||
|
{
|
|||
|
var pcrs = db.PCRs.ToList();
|
|||
|
var onePageOfPcrs = pcrs.ToPagedList(page, 10);
|
|||
|
return View(onePageOfPcrs);
|
|||
|
}
|
|||
|
|
|||
|
public ActionResult Edit(int id, int? copyfrom)
|
|||
|
{
|
|||
|
var Copy = copyfrom == null ? new PCR() : db.PCRs.Where(x => x.ID == copyfrom).Single();
|
|||
|
var viewModel = id == 0 ? copyfrom == null ?
|
|||
|
new PCR() :
|
|||
|
new PCR()
|
|||
|
{
|
|||
|
DisplayNameCN = Copy.DisplayNameCN,
|
|||
|
Percentage = Copy.Percentage,
|
|||
|
DisplayNameTW = Copy.DisplayNameTW,
|
|||
|
DisplayNameEN = Copy.DisplayNameEN,
|
|||
|
Description = Copy.Description
|
|||
|
} :
|
|||
|
db.PCRs.Where(x => x.ID == id).Single();
|
|||
|
return View(viewModel);
|
|||
|
}
|
|||
|
|
|||
|
[HttpPost]
|
|||
|
[Weee.Filter.MvcLog]
|
|||
|
public ActionResult Edit(int id, PCR viewModel)
|
|||
|
{
|
|||
|
if (ModelState.IsValid && id == viewModel.ID)
|
|||
|
{
|
|||
|
if (CheckNameExist(viewModel.DisplayNameTW, id))
|
|||
|
{
|
|||
|
ModelState.AddModelError(string.Empty, "\"" + viewModel.DisplayNameTW + "\"已存在");
|
|||
|
return View(viewModel);
|
|||
|
}
|
|||
|
db.Entry(viewModel).State = id == 0 ? EntityState.Added : EntityState.Modified;
|
|||
|
db.SaveChanges();
|
|||
|
return RedirectToAction("index");
|
|||
|
}
|
|||
|
return View(viewModel);
|
|||
|
}
|
|||
|
|
|||
|
[HttpGet]
|
|||
|
[Weee.Filter.MvcLog]
|
|||
|
public ActionResult Delete(int id)
|
|||
|
{
|
|||
|
db.Entry(new PCR() { ID = id }).State = EntityState.Deleted;
|
|||
|
db.SaveChanges();
|
|||
|
|
|||
|
return RedirectToAction("index");
|
|||
|
}
|
|||
|
|
|||
|
private bool CheckNameExist(string name, int id)
|
|||
|
{
|
|||
|
var exist = (from a in db.PCRs
|
|||
|
where a.DisplayNameTW == name && a.ID != id
|
|||
|
select a).Any();
|
|||
|
return exist;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|