public ActionResult Create([Bind(Include = "UserName,ContactListName")] UserContactListViewModel userContactListViewModel)
        {
            IContactListService _contactListService = new EfContactListService(_uow);

            if (ModelState.IsValid)
            {
                //ContactList contactList = contactListViewModel.MapViewModelToModel();
                UserContactList userContactList = new UserContactList();
                userContactList.UserId = _identityUserService.FindUserIdByName(userContactListViewModel.UserName);

                userContactList.ContactListId = _contactListService.Find(r => r.Name == userContactListViewModel.ContactListName).Id;
                if (!_userContactListService.Exist(userContactList.UserId, userContactList.ContactListId))
                {
                    _userContactListService.Add(userContactList);
                    _uow.SaveChanges();
                    return(Json(new { success = true }));
                }
                else
                {
                    ModelState.AddModelError("DuplicateRecord", "این کاربر به دفترچه تلفن دسترسی دارد");
                }
            }
            ViewBag.ContactListNames = new SelectList(_contactListService.GetAll(), "Name", "Name");
            IEnumerable <string> UserNames = _identityUserService.GetAllUserNames();

            ViewBag.UserNames = new SelectList(UserNames);
            return(PartialView("_Create", userContactListViewModel));
        }
        // GET: /ContactList/UserContactList/Details/5
        public ActionResult Details(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            UserContactList userContactList = _userContactListService.FindById((int)id);

            if (userContactList == null)
            {
                return(HttpNotFound());
            }
            UserContactListViewModel userContactListViewModel = userContactList.MapModelToViewModel();

            userContactListViewModel.UserName = _identityUserService.FindUserNameById(userContactList.UserId);
            return(PartialView("_Details", userContactListViewModel));
        }
        //public ActionResult Edit(int? id)
        //{
        //    if (id == null)
        //    {
        //        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        //    }
        //    UserContactList userContactList = _userContactListService.FindById((int)id);
        //    if (userContactList == null)
        //    {
        //        return HttpNotFound();
        //    }
        //    return PartialView("_Edit", userContactList.MapModelToViewModel());
        //}

        //// POST: /ContactList/UserContactList/Edit/5
        //// To protect from overposting attacks, please enable the specific properties you want to bind to, for
        //// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        //[HttpPost]
        //[ValidateAntiForgeryToken]
        //public ActionResult Edit([Bind(Include = "Id,UserName,ContactListName")] UserContactListViewModel userContactListViewModel)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        UserContactList userContactList = _userContactListService.FindById(userContactListViewModel.Id);
        //        userContactList.UserId = _identityUserService.FindUserIdByName(userContactListViewModel.UserName);

        //        IContactListService _contactListService = new EfContactListService(_uow);
        //        userContactList.ContactListId = _contactListService.Find(r => r.Name == userContactListViewModel.ContactListName).Id;
        //        _uow.SaveChanges();
        //        return Json(new { success = true });
        //    }
        //    return PartialView("_Edit", userContactListViewModel);
        //}

        // GET: /ContactList/UserContactList/Delete/5
        public ActionResult Delete(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            UserContactList userContactList = _userContactListService.FindById((int)id);

            if (userContactList == null)
            {
                return(HttpNotFound());
            }
            //UserContactListViewModel contactListViewModel = contactList.MapModelToViewModel();
            UserContactListViewModel contactListViewModel = new UserContactListViewModel();

            contactListViewModel.ContactListName = _contactListService.FindById(userContactList.ContactListId).Name;
            contactListViewModel.UserName        = _identityUserService.FindUserNameById(userContactList.UserId);
            return(PartialView("_Delete", contactListViewModel));
        }
Beispiel #4
0
 public static UserContactList MapViewModelToModel(this UserContactListViewModel entity)
 {
     return(Mapper.Map <UserContactListViewModel, UserContactList>(entity));
 }