Created
August 12, 2018 08:56
-
-
Save danielplawgo/9170a95c2aa676caf9db6da4d462b6b8 to your computer and use it in GitHub Desktop.
Jak zmienić generowanie kodu w ASP.NET MVC?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Products1Controller : Controller | |
{ | |
private DataContext db = new DataContext(); | |
// GET: /Products1/ | |
public ActionResult Index() | |
{ | |
return View(db.Products.ToList()); | |
} | |
// GET: /Products1/Details/5 | |
public ActionResult Details(int? id) | |
{ | |
if (id == null) | |
{ | |
return new HttpStatusCodeResult(HttpStatusCode.BadRequest); | |
} | |
Product product = db.Products.Find(id); | |
if (product == null) | |
{ | |
return HttpNotFound(); | |
} | |
return View(product); | |
} | |
// GET: /Products1/Create | |
public ActionResult Create() | |
{ | |
return View(); | |
} | |
// POST: /Products1/Create | |
// To protect from overposting attacks, please enable the specific properties you want to bind to, for | |
// more details see https://go.microsoft.com/fwlink/?LinkId=317598. | |
[HttpPost] | |
[ValidateAntiForgeryToken] | |
public ActionResult Create([Bind(Include="Id,Name,IsActive,CreatedDate,CreatedUser,UpdatedDate,UpdatedUser")] Product product) | |
{ | |
if (ModelState.IsValid) | |
{ | |
db.Products.Add(product); | |
db.SaveChanges(); | |
return RedirectToAction("Index"); | |
} | |
return View(product); | |
} | |
// GET: /Products1/Edit/5 | |
public ActionResult Edit(int? id) | |
{ | |
if (id == null) | |
{ | |
return new HttpStatusCodeResult(HttpStatusCode.BadRequest); | |
} | |
Product product = db.Products.Find(id); | |
if (product == null) | |
{ | |
return HttpNotFound(); | |
} | |
return View(product); | |
} | |
// POST: /Products1/Edit/5 | |
// To protect from overposting attacks, please enable the specific properties you want to bind to, for | |
// more details see https://go.microsoft.com/fwlink/?LinkId=317598. | |
[HttpPost] | |
[ValidateAntiForgeryToken] | |
public ActionResult Edit([Bind(Include="Id,Name,IsActive,CreatedDate,CreatedUser,UpdatedDate,UpdatedUser")] Product product) | |
{ | |
if (ModelState.IsValid) | |
{ | |
db.Entry(product).State = EntityState.Modified; | |
db.SaveChanges(); | |
return RedirectToAction("Index"); | |
} | |
return View(product); | |
} | |
// GET: /Products1/Delete/5 | |
public ActionResult Delete(int? id) | |
{ | |
if (id == null) | |
{ | |
return new HttpStatusCodeResult(HttpStatusCode.BadRequest); | |
} | |
Product product = db.Products.Find(id); | |
if (product == null) | |
{ | |
return HttpNotFound(); | |
} | |
return View(product); | |
} | |
// POST: /Products1/Delete/5 | |
[HttpPost, ActionName("Delete")] | |
[ValidateAntiForgeryToken] | |
public ActionResult DeleteConfirmed(int id) | |
{ | |
Product product = db.Products.Find(id); | |
db.Products.Remove(product); | |
db.SaveChanges(); | |
return RedirectToAction("Index"); | |
} | |
protected override void Dispose(bool disposing) | |
{ | |
if (disposing) | |
{ | |
db.Dispose(); | |
} | |
base.Dispose(disposing); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Products2Controller : Controller | |
{ | |
private Lazy<IProductLogic> _logic; | |
protected IProductLogic Logic | |
{ | |
get | |
{ | |
return _logic.Value; | |
} | |
} | |
private Lazy<IMapper> _mapper; | |
protected IMapper Mapper | |
{ | |
get | |
{ | |
return _mapper.Value; | |
} | |
} | |
public Products2Controller(Lazy<IProductLogic> logic, | |
Lazy<IMapper> mapper) | |
{ | |
this._logic = logic; | |
this._mapper = mapper; | |
} | |
// GET: /Products2/ | |
public ActionResult Index() | |
{ | |
var items = this.Logic.GetAllActive(); | |
var viewModel = new IndexViewModel(); | |
viewModel.Items = this.Mapper.Map<List<IndexItemViewModel>>(items); | |
return View(viewModel); | |
} | |
// GET: /Products2/Details/5 | |
public ActionResult Details(int? id) | |
{ | |
if (id == null) | |
{ | |
return new HttpStatusCodeResult(HttpStatusCode.BadRequest); | |
} | |
var model = this.Logic.GetById(id.Value); | |
if (model == null) | |
{ | |
return HttpNotFound(); | |
} | |
var viewModel = this.Mapper.Map<ProductViewModel>(model); | |
return View(viewModel); | |
} | |
// GET: /Products2/Create | |
public ActionResult Create() | |
{ | |
return View(); | |
} | |
// POST: /Products2/Create | |
// To protect from overposting attacks, please enable the specific properties you want to bind to, | |
// more details see https://go.microsoft.com/fwlink/?LinkId=317598. | |
[HttpPost] | |
[ValidateAntiForgeryToken] | |
public ActionResult Create(ProductViewModel viewModel) | |
{ | |
if (ModelState.IsValid) | |
{ | |
var model = this.Mapper.Map<Product>(viewModel); | |
this.Logic.Add(model); | |
return RedirectToAction("Index"); | |
} | |
return View(viewModel); | |
} | |
// GET: /Products2/Edit/5 | |
public ActionResult Edit(int? id) | |
{ | |
if (id == null) | |
{ | |
return new HttpStatusCodeResult(HttpStatusCode.BadRequest); | |
} | |
var model = this.Logic.GetById(id.Value); | |
if (model == null) | |
{ | |
return HttpNotFound(); | |
} | |
var viewModel = this.Mapper.Map<ProductViewModel>(model); | |
return View(viewModel); | |
} | |
// POST: /Products2/Edit/5 | |
// To protect from overposting attacks, please enable the specific properties you want to bind to, | |
// more details see https://go.microsoft.com/fwlink/?LinkId=317598. | |
[HttpPost] | |
[ValidateAntiForgeryToken] | |
public ActionResult Edit(ProductViewModel viewModel) | |
{ | |
if (ModelState.IsValid) | |
{ | |
var model = this.Logic.GetById(viewModel.Id); | |
if (model == null) | |
{ | |
return HttpNotFound(); | |
} | |
this.Mapper.Map(viewModel, model); | |
this.Logic.Update(model); | |
return RedirectToAction("Index"); | |
} | |
return View(viewModel); | |
} | |
// GET: /Products2/Delete/5 | |
public ActionResult Delete(int? id) | |
{ | |
if (id == null) | |
{ | |
return new HttpStatusCodeResult(HttpStatusCode.BadRequest); | |
} | |
var model = this.Logic.GetById(id.Value); | |
if (model == null) | |
{ | |
return HttpNotFound(); | |
} | |
var viewModel = this.Mapper.Map<ProductViewModel>(model); | |
return View(viewModel); | |
} | |
// POST: /Products2/Delete/5 | |
[HttpPost, ActionName("Delete")] | |
[ValidateAntiForgeryToken] | |
public ActionResult DeleteConfirmed(int id) | |
{ | |
this.Logic.Delete(id); | |
return RedirectToAction("Index"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment