I am looking to show a JavaScript alert with line breaks using the message returned from a controller action that returns JSON result. I have included "\n" in the message for line breaks.
Below is the code snippet from my controller:
[HttpPost]
public JsonResult CreatePO(PONewViewModel viewModel)
{
if (ModelState.IsValid)
{
var createPOResult = _managePOsAppServ
.CreateNewPOHeaderAndDetail(CurrentFacilityId, CurrentUserId, viewModel.VendorId,
viewModel.CustomerId, viewModel.OrderHeaderId, viewModel.ItemId, viewModel.QtyToOrder,
viewModel.UnitCost);
return Json(createPOResult, JsonRequestBehavior.AllowGet);
}
var modelStateErrors = this.ModelState.Keys.SelectMany(key => this.ModelState[key].Errors);
string errorMessage = "";
if (modelStateErrors.Count() > 0)
{
foreach (var error in modelStateErrors)
{
errorMessage += error.ErrorMessage + "\n";
}
}
return Json(ActionConfirmation<int>.CreateFailureConfirmation(errorMessage, -1,false).Message, JsonRequestBehavior.AllowGet);
}
But when displayed in the alert box, the \n is not recognized as a line break. How can I fix this?
JavaScript:
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
alert(xhr.responseText);
hideLoading();
}
}