Skip to content

Instantly share code, notes, and snippets.

@thiagomajesk
Created September 30, 2017 15:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thiagomajesk/a752c200eb16aaebbef2cf364387e10e to your computer and use it in GitHub Desktop.
Save thiagomajesk/a752c200eb16aaebbef2cf364387e10e to your computer and use it in GitHub Desktop.
ASP.NET Core PRG TempData Helpers
public class ModelStateTransferValue
{
public string Key { get; set; }
public string AttemptedValue { get; set; }
public object RawValue { get; set; }
public ICollection<string> ErrorMessages { get; set; } = new List<string>();
}
public static class ModelStateHelper
{
public static string SerialiseModelState(ModelStateDictionary modelState)
{
var errorList = modelState.Select(kvp => new ModelStateTransferValue
{
Key = kvp.Key,
AttemptedValue = kvp.Value.AttemptedValue,
RawValue = kvp.Value.RawValue,
ErrorMessages = kvp.Value.Errors.Select(err => err.ErrorMessage).ToList(),
});
return JsonConvert.SerializeObject(errorList);
}
public static ModelStateDictionary DeserialiseModelState(string serialisedErrorList)
{
var errorList = JsonConvert.DeserializeObject<List<ModelStateTransferValue>>(serialisedErrorList);
var modelState = new ModelStateDictionary();
foreach (var item in errorList)
{
modelState.SetModelValue(item.Key, item.RawValue, item.AttemptedValue);
foreach (var error in item.ErrorMessages)
{
modelState.AddModelError(item.Key, error);
}
}
return modelState;
}
}
public abstract class ModelStateTransfer : ActionFilterAttribute
{
protected const string Key = nameof(ModelStateTransfer);
}
public class ExportModelStateAttribute : ModelStateTransfer
{
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
//Somente exporta se o ModelState nao for valido
if (!filterContext.ModelState.IsValid)
{
if (filterContext.HttpContext.IsAjaxRequest())
ProcessAjax(filterContext);
else ProcessNormal(filterContext);
}
base.OnActionExecuted(filterContext);
}
public void ProcessNormal(ActionExecutedContext filterContext)
{
//Exporta se estiver redirecionando
if (filterContext.Result is RedirectResult || filterContext.Result is RedirectToRouteResult
|| filterContext.Result is RedirectToActionResult)
{
var controller = filterContext.Controller as Controller;
if (controller != null && filterContext.ModelState != null)
{
var modelState = ModelStateHelper.SerialiseModelState(filterContext.ModelState);
controller.TempData[Key] = modelState;
}
}
}
public void ProcessAjax(ActionExecutedContext filterContext)
{
var controller = filterContext.Controller as Controller;
if (controller != null && filterContext.ModelState != null)
{
var modelState = ModelStateHelper.SerialiseModelState(filterContext.ModelState);
filterContext.Result = new JsonResult(modelState);
}
}
}
public class ImportModelStateAttribute : ModelStateTransfer
{
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
var controller = filterContext.Controller as Controller;
var serialisedModelState = controller?.TempData[Key] as string;
if (serialisedModelState != null)
{
//Somente importa se estivermos visualizando
if (filterContext.Result is ViewResult)
{
var modelState = ModelStateHelper.DeserialiseModelState(serialisedModelState);
filterContext.ModelState.Merge(modelState);
}
else
{
//Caso contrario, remove-o
controller.TempData.Remove(Key);
}
}
base.OnActionExecuted(filterContext);
}
}
@helderboone
Copy link

@thiagomajesk coloca o codigo de como esta sendo chamado no controller?

@aruss
Copy link

aruss commented Jul 9, 2018

Tried to run it with checkboxes? Will explode with following error InvalidOperationException: The parameter conversion from type 'Newtonsoft.Json.Linq.JArray' to type 'System.Boolean' failed because no type converter can convert between these types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment