Click here to Skip to main content
15,884,009 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
In my controller I have this function:
Function AppError() As ActionResult
          Dim myScript As Object = "popup(" + Properties.postError + ")"
          Return RedirectToAction("main", myScript)
      End Function

With this function I want to call my razor page where I have a Javascript named `popup` whith a parameter `Properties.postError`.

My script is:
<script type="text/javascript">
       var errMsg;
       function popup(msg) {
           errMsg = msg;
           window.alert(errMsg);
           window.window.focus();
           if (window.confirm)
               errmsg = '';
           msg = '';
       }
   </script>

What I need is to popup the error message.
I follow the programm throu my debugger and I saw that it goes to my page.
But no error message window raise up.
I have already search in the web for this option without any success.
Is someone to assist me on this issue?

What I have tried:

ADDITION - 16/2/19 12:50
what I have done so far is the following:

Function CBError() As ActionResult
        Dim myScript As Object = "popup(" + Properties.postError + ")"
        ViewData.Add(New KeyValuePair(Of String, Object)("ScriptName", myScript))
        Return View("main", myScript)
    End Function

As we can see I've change the function by addiding the ViewData
I Add a value pair arguments 1) the name 2) the value
The action of addition it works just fine and the dictionery receive it.
I my script (which is in my reazor page) I put the following:

<script type="text/javascript">
   var vd = @ViewData.Item(0);
       function popup(msg) {
            var errMsg;
        errMsg = msg;
        window.alert(errMsg);
        window.window.focus();
        if (window.confirm)
            errmsg = '';
        msg = '';
        //{ window.location.href = "/" }
    }
</script>

When I stop (with the bebugger) in the instruction var vd = @ViewData.Item(0); I see that the ViewData is complitely empty. No values no nothing.
I also add this window.onload = @Html.Raw(ViewBag.MyScript);
And in debugger threw me an error

"Uncaught SyntaxError: Unexpected token ;"



.
Posted
Updated 21-Feb-19 6:26am
v2
Comments
F-ES Sitecore 18-Feb-19 5:35am    
View the source of the page in the browser and see what the javascript looks like, that might give you a clue as to why it isn't working.

You're running into VB's implicit type coercion nightmare.
  • The ViewData property is a ViewDataDictionary object[^], which is a dictionary with a string key.
  • You are adding an item with the key "ScriptName";
  • You are trying to retrieve an item with the key 0. VB "helpfully" converts that to the string "0" for you;
  • There is no item in the dictionary with the key "0".


You have to use the same key to retrieve the item that you used when you added it:
@ViewData("ScriptName")
 
Share this answer
 
The issue has solved by using ajax script...
But thanks any way...
Now I post another question on which nobody gives an answer (until now)
So lets try to solve this issue... which is very important...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900