ASP.NET MVC:ModelStateのエラーメッセージ一覧を取得してJsonで渡す
普段Modelのバリデーションチェックをして、エラーを格納するとき、以下のようにエラーメッセージを入れて
ModelState.AddModelError("", "○○が見つかりません。再度お試しください。");
ViewでValidationSummaryやValidationMessageを使ってエラーメッセージを表示します。
@Html.ValidationSummary() @Html.ValidationMessage("hoge")
が、Ajaxでjsonの戻り値として処理を終えたModelStateのエラーメッセージだけを取得し、画面に表示したいときもあります。
ModelStateのエラーメッセージ一覧を取得する
Jsonで結果を渡すためのモデル
public class JsonResultModel { public List<string> ErrorList { get; set; } public bool IsSuccess { get; set; } public CustomerBulkCreateViewModel() { ErrorList = new List<string>(); } }
アクション
[HttpPost] [ValidateAntiForgeryToken] public ActionResult CallAction(string id) { var model = new JsonResultModel(); // やりたい処理 // modelStateにあるエラーメッセージを画面上に表示させるためにmodel.ErrorListに格納 model.ErrorList = ModelState.Values.SelectMany(e => e.Errors.Select(er => er.ErrorMessage)).ToList(); return Json(model, "text/json", Encoding.UTF8, JsonRequestBehavior.AllowGet); }
手っ取り早くただのstringリストにエラーメッセージをぶち込みのも出きるけど、バリデーション処理をModelStateに集約しておくとロジック部分の汎用性も高まるし、MVC本来の機能も使えるので出来ればエラーを拾うのはModelStateを活用した方が良いと思います。
EntityFramework:Interceptor
参考サイト
www.entityframeworktutorial.net
EntityFrameworkのInterceptor作成例
EFCommandInterceptor.cs
class EFCommandInterceptor: IDbCommandInterceptor // ★ { public void NonQueryExecuted(System.Data.Common.DbCommand command, DbCommandInterceptionContext<int> interceptionContext) { LogInfo("NonQueryExecuted", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText)); } public void NonQueryExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<int> interceptionContext) { LogInfo("NonQueryExecuting", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText)); } public void ReaderExecuted(System.Data.Common.DbCommand command, DbCommandInterceptionContextt<System.Data.Common.DbDataReader> interceptionContext) { LogInfo("ReaderExecuted", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText)); } public void ReaderExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<System.Data.Common.DbDataReader> interceptionContext) { LogInfo("ReaderExecuting", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText)); } public void ScalarExecuted(System.Data.Common.DbCommand command, DbCommandInterceptionContext<object> interceptionContext) { LogInfo("ScalarExecuted", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText)); } public void ScalarExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<object> interceptionContext) { LogInfo("ScalarExecuting", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText)); } private void LogInfo(string command, string commandText) { Console.WriteLine("Intercepted on: {0} :- {1} ", command, commandText); } }
Global.asax.cs
public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { // filter, routeなどの設定諸々 DbInterception.Add(new EFCommandInterceptor()); // ★ } }
以上