Reputation: 41
I have this method that passes a List if the error occurs:
if (userList.Find(U => U.Id == userViewModel.Id) != null)
{
_response.ApiErrorList = new List<string> { "User already exist" };
_response.StatusCode = HttpStatusCode.BadRequest;
return BadRequest(_response);
}
I serialize the list and pass it to the TempData["ApiErrorList"]
if (ApiResponse.StatusCode == HttpStatusCode.BadRequest)
{
if (ApiResponse.ApiErrorList.Count > 0)
{
TempData["ApiErrorList"] = JsonConvert.SerializeObject(ApiResponse.ApiErrorList);
return RedirectToAction(redirectViewName, userViewModel);
}
I pass TempData to the ViewBag to pass it to the view:
if (TempData["ApiErrorList"] != null)
{
ViewBag.ApiErrorList = TempData["ApiErrorList"];
}
In the HTML page I inserted this code:
<input type="hidden" id="apiErrorList" data-value1="@ViewBag.ApiErrorList" />
In the external javascript page I inserted this code:
var apiErrors = $("#apiErrorList").data("value1");
but apiErrors is undefined.
<input type="hidden" id="apiErrorList" data-value1="["User already exist"]">
This is the result in the html page when the hidden field is valorised with the viewbag, why is the javascript part unable to take the value in the data-value1 field?
The problem is that apiErrors is always undefined
Upvotes: 0
Views: 37