Ibrahim Amer
Ibrahim Amer

Reputation: 1218

Access string stored in a ViewBag on ajax success

I'm pretty new to ASP.NET MVC, I been searching for a solution for this problem but I couldn't find any proper solution. I found some solutions here on stachoverflow but nothing has worked with me. Here are some links:

Possible to access MVC ViewBag object from Javascript file?

MVC 3 - Assign ViewBag Contents to Javascript string

Here is my ajax call to the server:

var xhr = new XMLHttpRequest();
        xhr.open('POST', '/Prize/UploadPassport');
        xhr.send(formdata);
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && xhr.status == 200) {
                var data = JSON.parse(xhr.responseText)
                if (data.nationality != "") {
                    $('#PassportData tbody').append('<tr><td data-title="@Web.Resources.MyResources.PassportNationality">' + data.nationality + '</td><td data-title="@Web.Resources.MyResources.PassportName">' + data.passportName + '</td><td><a><i id="viewApp_' + data.passportID + '" class="fa fa-search fa-lg" onclick="ViewPassport(' + data.passportID + ');"> <iframe id="img_' + data.passportID + '" class="costumeiframe"></iframe></i></a></td></tr>');
                }
                else {
                    //var errorMsg = data.errorMsg;
                    ShowDataValidationMessage("@ViewBag.FileError"); //here i'm getting an empty string
                }
            }
        }

In my server side action I set ViewBag.FileError based on some conditions here it is:

public ActionResult UploadPassport(HttpPostedFileBase FileUpload, string PassportCopyNationality)
    {

            if (Condition)
            {
                //Database access
            }

            else
            {
                if (isFileAlreadyExist)
                {
                    ViewBag.FileError = Web.Resources.MyResources.PassportAttachmentValidationForFile;
                }
                else if (file.ContentLength > 3145728 || !isFileTypeLegal)
                {
                    ViewBag.FileError = Web.Resources.MyResources.FileError;
                }

                return Json(new { nationality = "", passportName = "", passportID = "" });
            }


        }
        catch (IOException io)
        {

            return Json("File not uploaded");
        }
    }

The problem that I'm getting an empty string

Upvotes: 0

Views: 5476

Answers (1)

user3559349
user3559349

Reputation:

Firstly, @ViewBag.FileError (inside your script) is razor code which is parsed on the server before your view is sent to the client, so unless you include ViewBag.FileError = someValue in the GET method that generates this view, then it will always equate to null.

Secondly, your UploadPassport() method is returning a JsonResult not a view, so ViewBag does not even exist. You can resolve this by adding the value to the JsonResult, for example

 return Json(new { fileError = someValue, nationality = "", passportName = "", passportID = "" });

and then access it in the script

ShowDataValidationMessage("data.fileError");

Upvotes: 5

Related Questions