コード例 #1
0
ファイル: NoteController.cs プロジェクト: Hixon10/Notes
        public PartialViewResult GetFormForCreate()
        {
            var noteViewModel = new CreateNoteViewModel();

            var selectList = unitOfWork.NoteTypeRepository.Get().Select(x => new SelectListItem
            {
                Text = x.Type,
                Value = x.Id.ToString()
            }).ToList();

            noteViewModel.NoteType = selectList;

            return PartialView(noteViewModel);
        }
コード例 #2
0
ファイル: NoteController.cs プロジェクト: Hixon10/Notes
        public String Create(String data, int idNoteType)
        {
            String statusMessage;
            String jsonString;
            var scriptSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();

            CreateNoteViewModel createNoteViewModel = new CreateNoteViewModel();
            createNoteViewModel.Data = data;
            createNoteViewModel.IdNoteType = idNoteType;

            Note note = new Note();
            note.Data = createNoteViewModel.Data;
            note.NoteStatus = unitOfWork.NoteStatusRepository.GetByID(2);
            note.NoteType = unitOfWork.NoteTypeRepository.GetByID(createNoteViewModel.IdNoteType);

            String login = User.Identity.Name;

            User user = unitOfWork.UserRepository.Get().Where(a => a.Login.Trim() == login).FirstOrDefault();
            note.User = user;

            try
            {
                ValidateModel(note);

            }
            catch (Exception e)
            {
                statusMessage = "fail";
                var errors = ModelState
                            .Where(x => x.Value.Errors.Count > 0)
                            .Select(x => new { x.Key, x.Value.Errors })
                            .ToArray();

                jsonString = scriptSerializer.Serialize(new { status = statusMessage, errors = errors });
                return jsonString;
            }

            if (ModelState.IsValid)
            {
                unitOfWork.NoteRepository.Insert(note);
                try
                {
                    unitOfWork.Save();
                }
                catch (DbEntityValidationException dbEx)
                {
                    foreach (var validationErrors in dbEx.EntityValidationErrors)
                    {
                        foreach (var validationError in validationErrors.ValidationErrors)
                        {
                            Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
                        }
                    }
                }

                statusMessage = "success";
                String noteData = note.Data;
                String noteType = note.NoteType.Type;
                int idNote = note.Id;

                jsonString = scriptSerializer.Serialize(new { status = statusMessage, data = noteData, noteType = noteType, idNote = idNote });
                return jsonString;
            }

            statusMessage = "fail";

            jsonString = scriptSerializer.Serialize(new { status = statusMessage });
            return jsonString;
        }