Beispiel #1
0
        public void TrySavePost()
        {
            PostValidator validator = new PostValidator();
            Post          post      = new Post()
            {
                // Map form fields to post properties
                Id    = Convert.ToInt32(PostId.Value),
                Title = PostTitle.Text.Trim(),
                Body  = PostBody.Text.Trim()
            };
            ValidationResult results = validator.Validate(post);

            if (results.IsValid)
            {
                _postRepository.SavePost(post);
            }
            else
            {
                BulletedList summary = (BulletedList)FindControl("ErrorSummary");

                // Display errors to the user
                foreach (var failure in results.Errors)
                {
                    Label errorMessage = FindControl(failure.PropertyName + "Error") as Label;

                    if (errorMessage == null)
                    {
                        summary.Items.Add(new ListItem(failure.ErrorMessage));
                    }
                    else
                    {
                        errorMessage.Text = failure.ErrorMessage;
                    }
                }
            }
        }
        public PostControl()
        {
            _postRepository = new PostRepository();

            _validator = new PostValidator();
        }