admin管理员组文章数量:1419168
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 yourJsonResult
. 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 returnnull
– 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
1 Answer
Reset to default 5Firstly, @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");
本文标签: javascriptAccess string stored in a ViewBag on ajax successStack Overflow
版权声明:本文标题:javascript - Access string stored in a ViewBag on ajax success - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745304211a2652548.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论