Code Snippet
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Data.Entity;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using ITHotList.Models;
- using System.Web.Security;
- namespace ITHotList.Controllers
- {
- public class HotListController : Controller
- {
- private HotListEntities db = new HotListEntities();
- //
- // GET: /HotList/
- [Authorize]
- public ViewResult Index()
- {
- MembershipUser user = Membership.GetUser();
- Guid userId = (Guid)user.ProviderUserKey;
- return View(db.HotLists.Where(hotlist => hotlist.UserId == userId).OrderByDescending(hotlist => hotlist.CreateDate).ToList());
- }
- //
- // GET: /HotList/Details/5
- [Authorize]
- public ViewResult Details(int id)
- {
- HotList hotlist = db.HotLists.Find(id);
- return View(hotlist);
- }
- //
- // GET: /HotList/Create
- [Authorize]
- public ActionResult Create()
- {
- HotList hotlistModel = new HotList();
- hotlistModel.Name = "Name";
- hotlistModel.Resources.Add(new Resource() { Name = "", Availability= "", CurrentLocation="", ImmigrationStatus ="", JobRole="", PreferredLocation="", Rate ="", LinkToResume="", Skill="", YearsOfExp = 0 });
- return View(hotlistModel);
- }
- //
- // POST: /HotList/Create
- [Authorize]
- [HttpPost]
- public JsonResult Create(HotList receivedhotlist)
- {
- // never trust what has come from ui.
- string message = string.Empty;
- try
- {
- HotList hotlist = new HotList();
- if (ModelState.IsValid)
- {
- MembershipUser user = Membership.GetUser();
- Guid userId = (Guid)user.ProviderUserKey;
- hotlist.UserId = userId;
- try
- {
- hotlist.Active = true;
- hotlist.Name = receivedhotlist.Name;
- hotlist.CreateDate = DateTime.Now;
- var profile = Profile.GetProfile(user.UserName);
- if (profile.CompanyName != null)
- {
- hotlist.CompanyName = profile.CompanyName;
- }
- if (user.Email != null)
- {
- hotlist.Email = user.Email;
- }
- if (profile.PrimaryPhoneNumber != null)
- {
- hotlist.PrimaryPhoneNumber = profile.PrimaryPhoneNumber;
- }
- if (profile.PrimaryExt != null)
- {
- hotlist.PrimaryExt = profile.PrimaryExt;
- }
- if (profile.SecondaryPhoneNumber != null)
- {
- hotlist.SecondaryPhoneNumber = profile.SecondaryPhoneNumber;
- }
- if (profile.SecondaryExt != null)
- {
- hotlist.SecondaryExt = profile.SecondaryExt;
- }
- if (profile.Fax != null)
- {
- hotlist.Fax = profile.Fax;
- }
- // Error is happening here.. need to fix it.
- }
- catch { }
- foreach (Resource receivedresource in receivedhotlist.Resources)
- {
- Resource resource = new Resource();
- resource.Active = true;
- resource.UserId = userId;
- resource.JobRole = receivedresource.JobRole;
- resource.Availability = receivedresource.Availability;
- resource.CurrentLocation = receivedresource.CurrentLocation;
- resource.ImmigrationStatus = receivedresource.ImmigrationStatus;
- resource.LinkToResume = receivedresource.LinkToResume;
- resource.Name = receivedresource.Name;
- resource.PreferredLocation = receivedresource.PreferredLocation;
- resource.Rate = receivedresource.Rate;
- resource.Skill = receivedresource.Skill;
- resource.YearsOfExp = receivedresource.YearsOfExp;
- resource.HotLists.Add(hotlist);
- hotlist.Resources.Add(resource);
- db.Resources.Add(resource);
- }
- db.HotLists.Add(hotlist);
- db.SaveChanges();
- //do the persistence logic here
- message = "SUCCESS";
- }
- else
- {
- message = "modelstate is invalid";
- }
- }
- catch (Exception ex)
- {
- message = ex.Message.ToString();
- }
- return Json(message);
- }
- //[HttpPost]
- //public ActionResult Create(HotList hotlist)
- //{
- // if (ModelState.IsValid)
- // {
- // MembershipUser user = Membership.GetUser();
- // Guid userId = (Guid)user.ProviderUserKey;
- // hotlist.UserId = userId;
- // hotlist.CreateDate = DateTime.Now;
- // db.HotLists.Add(hotlist);
- // db.SaveChanges();
- // return RedirectToAction("Index");
- // }
- // return View(hotlist);
- //}
- //
- // GET: /HotList/Edit/5
- [Authorize]
- public ActionResult Edit(int id)
- {
- HotList hotlist = db.HotLists.Find(id);
- return View(hotlist);
- }
- //
- // POST: /HotList/Edit/5
- [Authorize]
- [HttpPost]
- public ActionResult Edit(HotList hotlist)
- {
- if (ModelState.IsValid)
- {
- db.Entry(hotlist).State = EntityState.Modified;
- db.SaveChanges();
- return RedirectToAction("Index");
- }
- return View(hotlist);
- }
- //
- // GET: /HotList/Delete/5
- [Authorize]
- public ActionResult Delete(int id)
- {
- HotList hotlist = db.HotLists.Find(id);
- return View(hotlist);
- }
- //
- // POST: /HotList/Delete/5
- [Authorize]
- [HttpPost, ActionName("Delete")]
- public ActionResult DeleteConfirmed(int id)
- {
- HotList hotlist = db.HotLists.Find(id);
- db.HotLists.Remove(hotlist);
- db.SaveChanges();
- return RedirectToAction("Index");
- }
- protected override void Dispose(bool disposing)
- {
- db.Dispose();
- base.Dispose(disposing);
- }
- }
- }
No comments:
Post a Comment