kreta/KretaWeb/Helpers/ModelStateDictionaryExtenstions.cs
2024-03-13 00:33:46 +01:00

41 lines
1.1 KiB
C#

using System.Linq;
using System.Web.Http.ModelBinding;
using Newtonsoft.Json;
namespace Kreta.Web.Helpers
{
public static class ModelStateDictionaryExtenstions
{
public static void AddModelStateErrorsFromString(this ModelStateDictionary modelState, string errors)
{
if (string.IsNullOrWhiteSpace(errors))
{
return;
}
var errorContent = JsonConvert.DeserializeObject<HttpClientErrorContent>(errors);
if (errorContent != null)
{
var errorList = errorContent.Message.Split(';');
if (errorList.Any())
{
int i = 1;
foreach (var error in errorList)
{
if (!string.IsNullOrWhiteSpace(error))
{
modelState.AddModelError(i.ToString(), error);
i++;
}
}
}
}
}
}
public class HttpClientErrorContent
{
public string Message { get; set; }
}
}