using EnVisage.Code.Extensions;
using System;
using System.Collections.Generic;
using System.IO;
using System.Web.Mvc;
namespace EnVisage.Code.Validation
{
///
/// Represents an information about field which did not pass validation.
///
public class FieldValidationError
{
///
/// Gets ir sets a name of the filed which did not pass validation.
///
public string FieldName { get; set; }
///
/// Gets or sets an validation error message, indicating what is wrong with the field value.
///
public List ErrorMessages { get; set; }
public FieldValidationError()
{
ErrorMessages = new List();
}
}
///
/// Represents a general JSON response used in Envisage website.
///
public class JsonResultBase : System.Web.Mvc.JsonResult
{
///
/// Gets or sets a value indicating whether validation failed or not. true - validation passed, false - validation failed.
///
//public bool ResultStatus { get; set; }
///
/// Gets or sets an error message.
///
//public List Errors { get; set; }
///
/// Gets or sets a value indicating any system specific error code. null - if not set.
///
//public int? ErrorCode { get; set; }
public JsonResultBase(bool status, List errors)
{
Data = new
{
Status = status,
Errors = errors
};
this.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
}
}
///
/// Represents a general JSON validation failed response.
///
public class FailedJsonResult : JsonResultBase
{
///
/// Creates an insteance of with empty field validation failed messages collection.
///
public FailedJsonResult() : base(false, new List()) { }
///
/// Creates an insteance of with prefilled field validation failed messages.
///
///
public FailedJsonResult(List errors) : base(false, errors) { }
///
/// Creates an insteance of with prefilled field validation failed messages.
///
///
public FailedJsonResult(ModelStateDictionary modelState) : base(false, null)
{
var errorModel = modelState.GetModelStateErrors();
Data = new
{
Status = false,
Errors = errorModel
};
}
}
///
/// Represents an empty JSON successfull response. Similar to new HttpStatusCodeResult(HttpStatusCode.OK).
///
public class SuccessJsonResult : JsonResultBase
{
public SuccessJsonResult() : base(true, null) { }
}
///
/// Represents an JSON successfull response with some data that should be sent to the client.
///
public class SuccessContentJsonResult : SuccessJsonResult
{
public SuccessContentJsonResult(object content)
: base()
{
Data = new
{
Status = true,
Content = content
};
}
}
///
/// Represents a JSON response which contains partial view content. Similar to new PartialView("_somePartialView", model)
///
public class PartialViewJsonResult : JsonResultBase
{
///
/// Creates an instance of class that renders a partial view, by using the specified view name and model.
///
/// 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.
/// The model that is rendered by the partial view.
/// Encapsulates information about the HTTP request.
/// The dictionary that contains the data that is required in order to render the view.
/// The dictionary that contains temporary data for the view.
/// return new PartialViewJsonResult("_editUser", edituser.aspnetuser, ControllerContext, ViewData, TempData);
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,
};
}
}
}