javascript - Access string stored in a ViewBag on ajax success - Stack Overflow

I'm pretty new to ASP.NET MVC, I been searching for a solution for this problem but I couldn'

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

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

Share Improve this question edited May 23, 2017 at 12:24 CommunityBot 11 silver badge asked Jul 29, 2015 at 23:47 Ibrahim AmerIbrahim Amer 1,2164 gold badges20 silver badges48 bronze badges 2
  • 1 Your passing json back to the client, not a view so ViewBag does not even exist. Just add the value to your JsonResult. Not to mention which @ViewBag.FileError is razor code which is parsed on the server before you pass the view to the client (in the initial page) so unless you set the value in the initial GET method, it would always return null – user3559349 Commented Jul 29, 2015 at 23:55
  • @StephenMuecke sorry for that I been on rush !! – Ibrahim Amer Commented Jul 31, 2015 at 13:53
Add a ment  | 

1 Answer 1

Reset to default 5

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");

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745304212a4621613.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信