141 lines
5.8 KiB
C#
141 lines
5.8 KiB
C#
using EnVisage.Code.Extensions;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Web.Mvc;
|
|
|
|
namespace EnVisage.Code.Validation
|
|
{
|
|
/// <summary>
|
|
/// Represents an information about field which did not pass validation.
|
|
/// </summary>
|
|
public class FieldValidationError
|
|
{
|
|
/// <summary>
|
|
/// Gets ir sets a name of the filed which did not pass validation.
|
|
/// </summary>
|
|
public string FieldName { get; set; }
|
|
/// <summary>
|
|
/// Gets or sets an validation error message, indicating what is wrong with the field value.
|
|
/// </summary>
|
|
public List<string> ErrorMessages { get; set; }
|
|
|
|
public FieldValidationError()
|
|
{
|
|
ErrorMessages = new List<string>();
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// Represents a general JSON response used in Envisage website.
|
|
/// </summary>
|
|
public class JsonResultBase : System.Web.Mvc.JsonResult
|
|
{
|
|
/// <summary>
|
|
/// Gets or sets a value indicating whether validation failed or not. <b>true</b> - validation passed, <b>false</b> - validation failed.
|
|
/// </summary>
|
|
//public bool ResultStatus { get; set; }
|
|
/// <summary>
|
|
/// Gets or sets an error message.
|
|
/// </summary>
|
|
//public List<FieldValidationError> Errors { get; set; }
|
|
/// <summary>
|
|
/// Gets or sets a value indicating any system specific error code. <b>null</b> - if not set.
|
|
/// </summary>
|
|
//public int? ErrorCode { get; set; }
|
|
|
|
public JsonResultBase(bool status, List<FieldValidationError> errors)
|
|
{
|
|
Data = new
|
|
{
|
|
Status = status,
|
|
Errors = errors
|
|
};
|
|
this.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// Represents a general JSON validation failed response.
|
|
/// </summary>
|
|
public class FailedJsonResult : JsonResultBase
|
|
{
|
|
/// <summary>
|
|
/// Creates an insteance of <see cref="FailedJsonResult"/> with empty field validation failed messages collection.
|
|
/// </summary>
|
|
public FailedJsonResult() : base(false, new List<FieldValidationError>()) { }
|
|
/// <summary>
|
|
/// Creates an insteance of <see cref="FailedJsonResult"/> with prefilled field validation failed messages.
|
|
/// </summary>
|
|
/// <param name="errors"></param>
|
|
public FailedJsonResult(List<FieldValidationError> errors) : base(false, errors) { }
|
|
/// <summary>
|
|
/// Creates an insteance of <see cref="FailedJsonResult"/> with prefilled field validation failed messages.
|
|
/// </summary>
|
|
/// <param name="errors"></param>
|
|
public FailedJsonResult(ModelStateDictionary modelState) : base(false, null)
|
|
{
|
|
var errorModel = modelState.GetModelStateErrors();
|
|
Data = new
|
|
{
|
|
Status = false,
|
|
Errors = errorModel
|
|
};
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// Represents an empty JSON successfull response. Similar to new HttpStatusCodeResult(HttpStatusCode.OK).
|
|
/// </summary>
|
|
public class SuccessJsonResult : JsonResultBase
|
|
{
|
|
public SuccessJsonResult() : base(true, null) { }
|
|
}
|
|
/// <summary>
|
|
/// Represents an JSON successfull response with some data that should be sent to the client.
|
|
/// </summary>
|
|
public class SuccessContentJsonResult : SuccessJsonResult
|
|
{
|
|
public SuccessContentJsonResult(object content)
|
|
: base()
|
|
{
|
|
Data = new
|
|
{
|
|
Status = true,
|
|
Content = content
|
|
};
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// Represents a JSON response which contains partial view content. Similar to new PartialView("_somePartialView", model)
|
|
/// </summary>
|
|
public class PartialViewJsonResult : JsonResultBase
|
|
{
|
|
/// <summary>
|
|
/// Creates an instance of <see cref="PartialViewJsonResult"/> class that renders a partial view, by using the specified view name and model.
|
|
/// </summary>
|
|
/// <param name="partialViewUrl">Path to partial view. E.g. "~/Views/Shared/EditorTemplates/ProjectPartModel.cshtml" or just "_edit.cshtml" if view is in the same folder as caller controller context.</param>
|
|
/// <param name="model">The model that is rendered by the partial view.</param>
|
|
/// <param name="controllerContext">Encapsulates information about the HTTP request.</param>
|
|
/// <param name="viewData">The dictionary that contains the data that is required in order to render the view.</param>
|
|
/// <param name="tempData">The dictionary that contains temporary data for the view.</param>
|
|
/// <example>return new PartialViewJsonResult("_editUser", edituser.aspnetuser, ControllerContext, ViewData, TempData);</example>
|
|
public PartialViewJsonResult(bool status, object data, string partialViewUrl, object model, ControllerContext controllerContext, ViewDataDictionary viewData, TempDataDictionary tempData) : base(status, null)
|
|
{
|
|
viewData.Model = model;
|
|
var content = string.Empty;
|
|
using (var sw = new StringWriter())
|
|
{
|
|
var viewResult = ViewEngines.Engines.FindPartialView(controllerContext, partialViewUrl);
|
|
var viewContext = new ViewContext(controllerContext, viewResult.View,
|
|
viewData, tempData, sw);
|
|
viewResult.View.Render(viewContext, sw);
|
|
viewResult.ViewEngine.ReleaseView(controllerContext, viewResult.View);
|
|
content = sw.GetStringBuilder().ToString();
|
|
}
|
|
Data = new
|
|
{
|
|
Status = status,
|
|
Data = data,
|
|
Html = content,
|
|
};
|
|
}
|
|
}
|
|
} |