Pages

Friday, January 14, 2011

MVC Controller

using System;

using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using EFCore.Data;

namespace MVCUI.Controllers
{

// Contact is the Controller
public class ContactController : Controller
{
AdventureWorksEntities _ctx = new AdventureWorksEntities();
//
// GET: /Contact/
// For Pagination
public ActionResult Index(int? startRowIndex, int? maxRows)
{
if (startRowIndex == null || maxRows == null)
{

return RedirectToAction("Index", "Contact", new { startRowIndex = 0, maxRows = 20 });

}

ContactViewModel viewModel = new ContactViewModel
{

ContactList = _ctx.Contacts.OrderBy(cnt => cnt.ContactID).Skip(startRowIndex.Value).Take(maxRows.Value).ToList(),
Current = null,
TotalCount = _ctx.Contacts.Count(),
PageIndex = startRowIndex.Value / maxRows.Value,
NextPageIndex = GetNextPage(startRowIndex.Value, maxRows.Value, _ctx.Contacts.Count()),
PrevPageIndex = GetPrevPage(startRowIndex.Value, maxRows.Value)
};
return View(viewModel);
}

//
// GET: /Contact/Details/5

public ActionResult Details(int id)
{
return View(new ContactViewModel { Current = _ctx.Contacts.Single(cnt => cnt.ContactID == id) });
}

//
// GET: /Contact/Create

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

//
// POST: /Contact/Create
// POST: you can get all values manually via Form collection in the request objector from collection parameter
//
[HttpPost]
public ActionResult Create(FormCollection collection)
{
ContactViewModel model = new ContactViewModel
{
Current = new Contact()
};
// this.ModelState.AddModelError()
TryUpdateModel(model);
if (ModelState.IsValid)
{
model.Current.PasswordHash = "e32e324=";
model.Current.PasswordSalt = "w";
model.Current.rowguid = Guid.NewGuid();
model.Current.ModifiedDate = DateTime.Now;
_ctx.Contacts.AddObject(model.Current);
_ctx.SaveChanges();
model.Current = _ctx.Contacts.OrderByDescending(cnt => cnt.ContactID).FirstOrDefault();
return RedirectToAction("Details", new { id = model.Current.ContactID });
}
else
{
return View(model);

}


}





//
// GET: /Contact/Edit/5

public ActionResult Edit(int id)
{
return View(_ctx.Contacts.Single(cnt => cnt.ContactID == id));

}

//
// POST: /Contact/Edit/5

[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{

Contact contact = _ctx.Contacts.SingleOrDefault(cnt => cnt.ContactID == id);

if (string.IsNullOrEmpty(collection["FirstName"]))
{
ModelState.AddModelError("FirstName", "First name should not be Null");
}
if (ModelState.IsValid)
{
TryUpdateModel(contact);
_ctx.SaveChanges();

return RedirectToAction("Details", new { id = id });
}
else
{
return View(contact);

}


}

//
// GET: /Contact/Delete/5

public ActionResult Delete(int id)
{
return View();
}

//
// POST: /Contact/Delete/5

public ActionResult GetContactsJSON(string id)
{
if (string.IsNullOrEmpty(id))
{
return Json(_ctx.Contacts.OrderBy(cnt => cnt.FirstName).Take(100).ToList(), JsonRequestBehavior.AllowGet);
}
else
{

return Json(_ctx.Contacts.Where(cnt => cnt.FirstName.StartsWith(id)).OrderBy(cnt => cnt.FirstName).Take(100).ToList(), JsonRequestBehavior.AllowGet);

}
}
public ActionResult GetContactJSON(int id)
{

return Json(_ctx.Contacts.SingleOrDefault(cnt => cnt.ContactID == id), JsonRequestBehavior.AllowGet);

}
[HttpPost]
public ActionResult Delete(int id, FormCollection collection)
{
try
{
// TODO: Add delete logic here

return RedirectToAction("Index");
}
catch
{
return View();
}
}

private int GetPrevPage(int startRowIndex, int maxRows)
{
try
{
var result = startRowIndex - maxRows;
return result <= 0 ? 0 : result; } catch { return 0; }
}

private int GetNextPage(int startRowIndex, int maxRows, int totalCount)
{
try
{
var result = startRowIndex + maxRows;

return result >= totalCount ? totalCount - maxRows : result;
}
catch
{

return 0;
}
}
}

public class ContactViewModel
{
public long TotalCount { get; set; }
public List<Contact> ContactList { get; set; }
public Contact Current { get; set; }
public int PageIndex { get; set; }
public int NextPageIndex { get; set; }
public int PrevPageIndex { get; set; }


}
}

No comments:

Post a Comment