Error in my view

1

How do I play these messages in my view?

Code that returns the messages:

public ActionResult SaveInternalAuditRecord(InternalAuditRecord criticalAnalysisRecord, string idResponsibles, string idAuditors)
{
     if (!ModelState.IsValid)
    {
        ModelState.OrderByKeys(criticalAnalysisRecord.GetValidationOrder());
        Response.StatusCode = (int)HttpStatusCode.BadRequest;
        return Json(ModelState.GetModelStateErros());
    }
}
    
asked by anonymous 19.07.2017 / 13:18

1 answer

3

I think the best in this case would be to create a partialView where you would work the errors and then use it on the screen and return the PartialView;

Example:

1 - Partial with error: "_ValidationSummary.cshtml"

@if (!Html.ViewData.ModelState.IsValid)
{
    <div class="alert alert-danger" role="alert">
        <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <p class="text-center"><i class="fa fa-exclamation-triangle" aria-hidden="true"></i> Alerta!</p>
        <div class="validation-summary-errors">
                @foreach (var modelState in ViewData.ModelState.Values)
                {
                    foreach (ModelError error in modelState.Errors)
                    {
                        //aqui você tem seus erros. Trabalhe neles no padrão que precisar.
                    }
                }
        </div>
    </div>
}

Controller:

using System.IO;
using System.Linq;
using System.Web.Mvc;

public ActionResult SaveInternalAuditRecord(InternalAuditRecord criticalAnalysisRecord, string idResponsibles, string idAuditors)
{
     if (!ModelState.IsValid)
    {
        ModelState.OrderByKeys(criticalAnalysisRecord.GetValidationOrder());
        Response.StatusCode = (int)HttpStatusCode.BadRequest;
        return Json(new
            {
                RenderPartialToString("~/Views/Layout/_ValidationSummary.cshtml", null, ControllerContext, string.Empty)
            }, JsonRequestBehavior.AllowGet);;
    }
}

private string RenderPartialToString(string viewPath, object model, ControllerContext controllerContext, string partialFieldName)
        {
            var viewData = new ViewDataDictionary();
            var tempData = new TempDataDictionary();
            viewData.Model = model;

            if (!string.IsNullOrWhiteSpace(partialFieldName))
            {
                viewData.TemplateInfo.HtmlFieldPrefix = partialFieldName;
            }

            using (var sw = new StringWriter())
            {
                var viewResult = ViewEngines.Engines.FindPartialView(controllerContext, viewPath);
                var viewContext = new ViewContext(controllerContext, viewResult.View, viewData, tempData, sw);

                if (controllerContext != null)
                    foreach (var item in controllerContext.Controller.ViewData.ModelState.Where(item => item.Value.Errors.Any()))
                        viewContext.ViewData.ModelState.Add(item);

                viewResult.View.Render(viewContext, sw);

                return sw.GetStringBuilder().ToString();
            }
        }

You just replace it with your ajax, just as you already do in your code;

    
19.07.2017 / 15:47