{"action":"created","comment":{"url":"https://api.github.com/repos/aspnet/Mvc/pulls/comments/40129242","id":40129242,"diff_hunk":"@@ -0,0 +1,76 @@\n+﻿// Copyright (c) .NET Foundation. All rights reserved.\n+// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.\n+\n+using System;\n+using System.Linq.Expressions;\n+using Microsoft.AspNet.Mvc.ModelBinding;\n+using Microsoft.AspNet.Mvc.ViewFeatures;\n+\n+namespace Microsoft.AspNet.Mvc\n+{\n+    /// <summary>\n+    /// Extensions methods for <see cref=\"ModelStateDictionary\"/>.\n+    /// </summary>\n+    public static class ModelStateDictionaryExtensions\n+    {\n+        /// <summary>\n+        /// Adds the specified <paramref name=\"errorMessage\"/> to the <see cref=\"ModelState.Errors\"/> instance\n+        /// that is associated with the specified <paramref name=\"expression\"/> text key repressentation.\n+        /// </summary>\n+        /// <typeparam name=\"TModel\">The type of the model.</typeparam>\n+        /// <param name=\"modelState\">The <see cref=\"ModelStateDictionary\"/> instance this method extends.</param>\n+        /// <param name=\"expression\">The expression of <typeparamref name=\"TModel\"/> from\n+        /// which text representation will be used as a key of the <see cref=\"ModelState\"/> to add errors to.</param>\n+        /// <param name=\"errorMessage\"></param>\n+        public static void AddModelError<TModel>(this ModelStateDictionary modelState, Expression<Func<TModel, object>> expression, string errorMessage)\n+        {\n+            modelState.AddModelError(GetExpressionText(expression), errorMessage);\n+        }\n+\n+        /// <summary>\n+        /// Adds the specified <paramref name=\"exception\"/> to the <see cref=\"ModelState.Errors\"/> instance\n+        /// that is associated with the specified <paramref name=\"expression\"/> text key repressentation.\n+        /// </summary>\n+        /// <typeparam name=\"TModel\">The type of the model.</typeparam>\n+        /// <param name=\"modelState\">The <see cref=\"ModelStateDictionary\"/> instance this method extends.</param>\n+        /// <param name=\"expression\">The expression of <typeparamref name=\"TModel\"/> from\n+        /// which text representation will be used as a key of the <see cref=\"ModelState\"/> to add errors to.</param>\n+        /// <param name=\"exception\">The error message to add.</param>\n+        public static void AddModelError<TModel>(this ModelStateDictionary modelState, Expression<Func<TModel, object>> expression, Exception exception)\n+        {\n+            modelState.AddModelError(GetExpressionText(expression), exception);\n+        }\n+\n+        /// <summary>\n+        /// Removes the specified <paramref name=\"expression\"/> text key repressentation from the <see cref=\"ModelStateDictionary\"/>.\n+        /// </summary>\n+        /// <typeparam name=\"TModel\">The type of the model.</typeparam>\n+        /// <param name=\"modelState\">The <see cref=\"ModelStateDictionary\"/> instance this method extends.</param>\n+        /// <param name=\"expression\">The expression of <typeparamref name=\"TModel\"/> from\n+        /// which text representation will be used as a key of the <see cref=\"ModelState\"/> to add errors to.</param>\n+        /// <returns>\n+        /// true if the element is successfully removed; otherwise, false.\n+        /// This method also returns false if expression's text key was not found in the model-state dictionary.\n+        /// </returns>\n+        public static bool Remove<TModel>(this ModelStateDictionary modelState, Expression<Func<TModel, object>> expression)\n+        {\n+            return modelState.Remove(GetExpressionText(expression));","path":"src/Microsoft.AspNet.Mvc.ViewFeatures/ModelStateDictionaryExtensions.cs","position":57,"original_position":57,"commit_id":"c401c6ad9d3675935c77dffd7256d900d6a5ab62","original_commit_id":"c401c6ad9d3675935c77dffd7256d900d6a5ab62","user":{"login":"Muchiachio","id":6647444,"avatar_url":"https://avatars.githubusercontent.com/u/6647444?v=3","gravatar_id":"","url":"https://api.github.com/users/Muchiachio","html_url":"https://github.com/Muchiachio","followers_url":"https://api.github.com/users/Muchiachio/followers","following_url":"https://api.github.com/users/Muchiachio/following{/other_user}","gists_url":"https://api.github.com/users/Muchiachio/gists{/gist_id}","starred_url":"https://api.github.com/users/Muchiachio/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Muchiachio/subscriptions","organizations_url":"https://api.github.com/users/Muchiachio/orgs","repos_url":"https://api.github.com/users/Muchiachio/repos","events_url":"https://api.github.com/users/Muchiachio/events{/privacy}","received_events_url":"https://api.github.com/users/Muchiachio/received_events","type":"User","site_admin":false},"body":"> will remove only the entry for the top-level model and not for any bound property.\r\n\r\nCould you elaborate a bit more on this scenario? Because I couldn't completely understand what you meant here.","created_at":"2015-09-22T19:01:49Z","updated_at":"2015-09-22T19:01:49Z","html_url":"https://github.com/aspnet/Mvc/pull/3182#discussion_r40129242","pull_request_url":"https://api.github.com/repos/aspnet/Mvc/pulls/3182","_links":{"self":{"href":"https://api.github.com/repos/aspnet/Mvc/pulls/comments/40129242"},"html":{"href":"https://github.com/aspnet/Mvc/pull/3182#discussion_r40129242"},"pull_request":{"href":"https://api.github.com/repos/aspnet/Mvc/pulls/3182"}}},"pull_request":{"url":"https://api.github.com/repos/aspnet/Mvc/pulls/3182","id":45694525,"html_url":"https://github.com/aspnet/Mvc/pull/3182","diff_url":"https://github.com/aspnet/Mvc/pull/3182.diff","patch_url":"https://github.com/aspnet/Mvc/pull/3182.patch","issue_url":"https://api.github.com/repos/aspnet/Mvc/issues/3182","number":3182,"state":"open","locked":false,"title":"Generic ModelStateDictionary add and remove extensions","user":{"login":"Muchiachio","id":6647444,"avatar_url":"https://avatars.githubusercontent.com/u/6647444?v=3","gravatar_id":"","url":"https://api.github.com/users/Muchiachio","html_url":"https://github.com/Muchiachio","followers_url":"https://api.github.com/users/Muchiachio/followers","following_url":"https://api.github.com/users/Muchiachio/following{/other_user}","gists_url":"https://api.github.com/users/Muchiachio/gists{/gist_id}","starred_url":"https://api.github.com/users/Muchiachio/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Muchiachio/subscriptions","organizations_url":"https://api.github.com/users/Muchiachio/orgs","repos_url":"https://api.github.com/users/Muchiachio/repos","events_url":"https://api.github.com/users/Muchiachio/events{/privacy}","received_events_url":"https://api.github.com/users/Muchiachio/received_events","type":"User","site_admin":false},"body":"This adds generic add model error and remove extension methods for ModelStateDictionary, to avoid using hard coded strings then specifying model state dictionary keys. #3164\r\n\r\nThere are few concerns what comes to mind\r\n- Initial [thought](https://github.com/aspnet/Mvc/issues/3164#issuecomment-142053478) by @rynowak was to add this to `Microsoft.AspNet.Mvc.Core`, but I couldn't because of circular references, so for now it's in `Microsoft.AspNet.Mvc.ViewFeatures`.\r\n- Will three method be enough for covering most of the model state dictionary usage cases?\r\n- Does `Remove<TModel>(this ModelStateDictionary, Expression)` is clear enough, maybe extension method should be named RemoveKey?\r\n- Is adding and removing empty key on expressions without normal text representation is correct and naturally expected behavior?\r\n\r\n\r\n<!---\r\n@huboard:{\"order\":3028.794921875,\"milestone_order\":3182,\"custom_state\":\"\"}\n-->\r\n","created_at":"2015-09-22T17:48:19Z","updated_at":"2015-09-22T19:01:49Z","closed_at":null,"merged_at":null,"merge_commit_sha":"70154cb8e8dc447d44e7b8e5bd3de482a1240345","assignee":{"login":"dougbu","id":6431421,"avatar_url":"https://avatars.githubusercontent.com/u/6431421?v=3","gravatar_id":"","url":"https://api.github.com/users/dougbu","html_url":"https://github.com/dougbu","followers_url":"https://api.github.com/users/dougbu/followers","following_url":"https://api.github.com/users/dougbu/following{/other_user}","gists_url":"https://api.github.com/users/dougbu/gists{/gist_id}","starred_url":"https://api.github.com/users/dougbu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dougbu/subscriptions","organizations_url":"https://api.github.com/users/dougbu/orgs","repos_url":"https://api.github.com/users/dougbu/repos","events_url":"https://api.github.com/users/dougbu/events{/privacy}","received_events_url":"https://api.github.com/users/dougbu/received_events","type":"User","site_admin":false},"milestone":null,"commits_url":"https://api.github.com/repos/aspnet/Mvc/pulls/3182/commits","review_comments_url":"https://api.github.com/repos/aspnet/Mvc/pulls/3182/comments","review_comment_url":"https://api.github.com/repos/aspnet/Mvc/pulls/comments{/number}","comments_url":"https://api.github.com/repos/aspnet/Mvc/issues/3182/comments","statuses_url":"https://api.github.com/repos/aspnet/Mvc/statuses/c401c6ad9d3675935c77dffd7256d900d6a5ab62","head":{"label":"Muchiachio:dev","ref":"dev","sha":"c401c6ad9d3675935c77dffd7256d900d6a5ab62","user":{"login":"Muchiachio","id":6647444,"avatar_url":"https://avatars.githubusercontent.com/u/6647444?v=3","gravatar_id":"","url":"https://api.github.com/users/Muchiachio","html_url":"https://github.com/Muchiachio","followers_url":"https://api.github.com/users/Muchiachio/followers","following_url":"https://api.github.com/users/Muchiachio/following{/other_user}","gists_url":"https://api.github.com/users/Muchiachio/gists{/gist_id}","starred_url":"https://api.github.com/users/Muchiachio/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Muchiachio/subscriptions","organizations_url":"https://api.github.com/users/Muchiachio/orgs","repos_url":"https://api.github.com/users/Muchiachio/repos","events_url":"https://api.github.com/users/Muchiachio/events{/privacy}","received_events_url":"https://api.github.com/users/Muchiachio/received_events","type":"User","site_admin":false},"repo":{"id":42882533,"name":"Mvc","full_name":"Muchiachio/Mvc","owner":{"login":"Muchiachio","id":6647444,"avatar_url":"https://avatars.githubusercontent.com/u/6647444?v=3","gravatar_id":"","url":"https://api.github.com/users/Muchiachio","html_url":"https://github.com/Muchiachio","followers_url":"https://api.github.com/users/Muchiachio/followers","following_url":"https://api.github.com/users/Muchiachio/following{/other_user}","gists_url":"https://api.github.com/users/Muchiachio/gists{/gist_id}","starred_url":"https://api.github.com/users/Muchiachio/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Muchiachio/subscriptions","organizations_url":"https://api.github.com/users/Muchiachio/orgs","repos_url":"https://api.github.com/users/Muchiachio/repos","events_url":"https://api.github.com/users/Muchiachio/events{/privacy}","received_events_url":"https://api.github.com/users/Muchiachio/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/Muchiachio/Mvc","description":"Model view controller framework for building dynamic web sites with clean separation of concerns, including the merged MVC, Web API, and Web Pages w/ Razor.","fork":true,"url":"https://api.github.com/repos/Muchiachio/Mvc","forks_url":"https://api.github.com/repos/Muchiachio/Mvc/forks","keys_url":"https://api.github.com/repos/Muchiachio/Mvc/keys{/key_id}","collaborators_url":"https://api.github.com/repos/Muchiachio/Mvc/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/Muchiachio/Mvc/teams","hooks_url":"https://api.github.com/repos/Muchiachio/Mvc/hooks","issue_events_url":"https://api.github.com/repos/Muchiachio/Mvc/issues/events{/number}","events_url":"https://api.github.com/repos/Muchiachio/Mvc/events","assignees_url":"https://api.github.com/repos/Muchiachio/Mvc/assignees{/user}","branches_url":"https://api.github.com/repos/Muchiachio/Mvc/branches{/branch}","tags_url":"https://api.github.com/repos/Muchiachio/Mvc/tags","blobs_url":"https://api.github.com/repos/Muchiachio/Mvc/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/Muchiachio/Mvc/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/Muchiachio/Mvc/git/refs{/sha}","trees_url":"https://api.github.com/repos/Muchiachio/Mvc/git/trees{/sha}","statuses_url":"https://api.github.com/repos/Muchiachio/Mvc/statuses/{sha}","languages_url":"https://api.github.com/repos/Muchiachio/Mvc/languages","stargazers_url":"https://api.github.com/repos/Muchiachio/Mvc/stargazers","contributors_url":"https://api.github.com/repos/Muchiachio/Mvc/contributors","subscribers_url":"https://api.github.com/repos/Muchiachio/Mvc/subscribers","subscription_url":"https://api.github.com/repos/Muchiachio/Mvc/subscription","commits_url":"https://api.github.com/repos/Muchiachio/Mvc/commits{/sha}","git_commits_url":"https://api.github.com/repos/Muchiachio/Mvc/git/commits{/sha}","comments_url":"https://api.github.com/repos/Muchiachio/Mvc/comments{/number}","issue_comment_url":"https://api.github.com/repos/Muchiachio/Mvc/issues/comments{/number}","contents_url":"https://api.github.com/repos/Muchiachio/Mvc/contents/{+path}","compare_url":"https://api.github.com/repos/Muchiachio/Mvc/compare/{base}...{head}","merges_url":"https://api.github.com/repos/Muchiachio/Mvc/merges","archive_url":"https://api.github.com/repos/Muchiachio/Mvc/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/Muchiachio/Mvc/downloads","issues_url":"https://api.github.com/repos/Muchiachio/Mvc/issues{/number}","pulls_url":"https://api.github.com/repos/Muchiachio/Mvc/pulls{/number}","milestones_url":"https://api.github.com/repos/Muchiachio/Mvc/milestones{/number}","notifications_url":"https://api.github.com/repos/Muchiachio/Mvc/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/Muchiachio/Mvc/labels{/name}","releases_url":"https://api.github.com/repos/Muchiachio/Mvc/releases{/id}","created_at":"2015-09-21T17:34:20Z","updated_at":"2015-09-21T17:34:28Z","pushed_at":"2015-09-22T17:30:29Z","git_url":"git://github.com/Muchiachio/Mvc.git","ssh_url":"git@github.com:Muchiachio/Mvc.git","clone_url":"https://github.com/Muchiachio/Mvc.git","svn_url":"https://github.com/Muchiachio/Mvc","homepage":"","size":15644,"stargazers_count":1,"watchers_count":1,"language":"C#","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":1,"default_branch":"dev"}},"base":{"label":"aspnet:dev","ref":"dev","sha":"9eb2c5b810a3e51a010510dfb268ff28a99d3d05","user":{"login":"aspnet","id":6476660,"avatar_url":"https://avatars.githubusercontent.com/u/6476660?v=3","gravatar_id":"","url":"https://api.github.com/users/aspnet","html_url":"https://github.com/aspnet","followers_url":"https://api.github.com/users/aspnet/followers","following_url":"https://api.github.com/users/aspnet/following{/other_user}","gists_url":"https://api.github.com/users/aspnet/gists{/gist_id}","starred_url":"https://api.github.com/users/aspnet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aspnet/subscriptions","organizations_url":"https://api.github.com/users/aspnet/orgs","repos_url":"https://api.github.com/users/aspnet/repos","events_url":"https://api.github.com/users/aspnet/events{/privacy}","received_events_url":"https://api.github.com/users/aspnet/received_events","type":"Organization","site_admin":false},"repo":{"id":15126429,"name":"Mvc","full_name":"aspnet/Mvc","owner":{"login":"aspnet","id":6476660,"avatar_url":"https://avatars.githubusercontent.com/u/6476660?v=3","gravatar_id":"","url":"https://api.github.com/users/aspnet","html_url":"https://github.com/aspnet","followers_url":"https://api.github.com/users/aspnet/followers","following_url":"https://api.github.com/users/aspnet/following{/other_user}","gists_url":"https://api.github.com/users/aspnet/gists{/gist_id}","starred_url":"https://api.github.com/users/aspnet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aspnet/subscriptions","organizations_url":"https://api.github.com/users/aspnet/orgs","repos_url":"https://api.github.com/users/aspnet/repos","events_url":"https://api.github.com/users/aspnet/events{/privacy}","received_events_url":"https://api.github.com/users/aspnet/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/aspnet/Mvc","description":"Model view controller framework for building dynamic web sites with clean separation of concerns, including the merged MVC, Web API, and Web Pages w/ Razor.","fork":false,"url":"https://api.github.com/repos/aspnet/Mvc","forks_url":"https://api.github.com/repos/aspnet/Mvc/forks","keys_url":"https://api.github.com/repos/aspnet/Mvc/keys{/key_id}","collaborators_url":"https://api.github.com/repos/aspnet/Mvc/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/aspnet/Mvc/teams","hooks_url":"https://api.github.com/repos/aspnet/Mvc/hooks","issue_events_url":"https://api.github.com/repos/aspnet/Mvc/issues/events{/number}","events_url":"https://api.github.com/repos/aspnet/Mvc/events","assignees_url":"https://api.github.com/repos/aspnet/Mvc/assignees{/user}","branches_url":"https://api.github.com/repos/aspnet/Mvc/branches{/branch}","tags_url":"https://api.github.com/repos/aspnet/Mvc/tags","blobs_url":"https://api.github.com/repos/aspnet/Mvc/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/aspnet/Mvc/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/aspnet/Mvc/git/refs{/sha}","trees_url":"https://api.github.com/repos/aspnet/Mvc/git/trees{/sha}","statuses_url":"https://api.github.com/repos/aspnet/Mvc/statuses/{sha}","languages_url":"https://api.github.com/repos/aspnet/Mvc/languages","stargazers_url":"https://api.github.com/repos/aspnet/Mvc/stargazers","contributors_url":"https://api.github.com/repos/aspnet/Mvc/contributors","subscribers_url":"https://api.github.com/repos/aspnet/Mvc/subscribers","subscription_url":"https://api.github.com/repos/aspnet/Mvc/subscription","commits_url":"https://api.github.com/repos/aspnet/Mvc/commits{/sha}","git_commits_url":"https://api.github.com/repos/aspnet/Mvc/git/commits{/sha}","comments_url":"https://api.github.com/repos/aspnet/Mvc/comments{/number}","issue_comment_url":"https://api.github.com/repos/aspnet/Mvc/issues/comments{/number}","contents_url":"https://api.github.com/repos/aspnet/Mvc/contents/{+path}","compare_url":"https://api.github.com/repos/aspnet/Mvc/compare/{base}...{head}","merges_url":"https://api.github.com/repos/aspnet/Mvc/merges","archive_url":"https://api.github.com/repos/aspnet/Mvc/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/aspnet/Mvc/downloads","issues_url":"https://api.github.com/repos/aspnet/Mvc/issues{/number}","pulls_url":"https://api.github.com/repos/aspnet/Mvc/pulls{/number}","milestones_url":"https://api.github.com/repos/aspnet/Mvc/milestones{/number}","notifications_url":"https://api.github.com/repos/aspnet/Mvc/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/aspnet/Mvc/labels{/name}","releases_url":"https://api.github.com/repos/aspnet/Mvc/releases{/id}","created_at":"2013-12-12T03:54:00Z","updated_at":"2015-09-22T13:26:59Z","pushed_at":"2015-09-22T18:42:55Z","git_url":"git://github.com/aspnet/Mvc.git","ssh_url":"git@github.com:aspnet/Mvc.git","clone_url":"https://github.com/aspnet/Mvc.git","svn_url":"https://github.com/aspnet/Mvc","homepage":"","size":141962,"stargazers_count":2180,"watchers_count":2180,"language":"C#","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":806,"mirror_url":null,"open_issues_count":212,"forks":806,"open_issues":212,"watchers":2180,"default_branch":"dev"}},"_links":{"self":{"href":"https://api.github.com/repos/aspnet/Mvc/pulls/3182"},"html":{"href":"https://github.com/aspnet/Mvc/pull/3182"},"issue":{"href":"https://api.github.com/repos/aspnet/Mvc/issues/3182"},"comments":{"href":"https://api.github.com/repos/aspnet/Mvc/issues/3182/comments"},"review_comments":{"href":"https://api.github.com/repos/aspnet/Mvc/pulls/3182/comments"},"review_comment":{"href":"https://api.github.com/repos/aspnet/Mvc/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/aspnet/Mvc/pulls/3182/commits"},"statuses":{"href":"https://api.github.com/repos/aspnet/Mvc/statuses/c401c6ad9d3675935c77dffd7256d900d6a5ab62"}}}}