views:

57

answers:

1

New to MVC, trying to pass a variable from Flash using FSCommand (works well with other functions) from one view to another. The Javascript I am using:

function p1_DoFSCommand(command, args) {
            var p1Obj = InternetExplorer ? p1 : document.p1;
            if (command == "nameClip") {
                var  FlashName = [args];
            }

in the Homecontroller:

 public ActionResult Testing(string FlashName)
        {

            ViewData["Message"] = FlashName;

            return View();

        }

In the second view:

Html.Encode(ViewData["message"])

Would appreciate your assistance.

A: 

This should get the data to the Action method:

function p1_DoFSCommand(command, args) {
                var p1Obj = InternetExplorer ? p1 : document.p1;
                if (command == "nameClip") {
                    var  FlashName = [args];

                    // might have to replace '?' with '&'
                    var formData = "?FlashName=" + FlashName; 

                    $.post('/HomeController/Testing', formData, function(res)
                    {
                        // do stuff with response

                    }, "json");
    }

You'll also need to reference jQuery for this to work.

If you need to pass that value to another controller, simply change

$.post('/HomeController/Testing', formData, function(res)

to

$.post('/OtherController/Testing', formData, function(res)

edit:
I'm not sure exactly what you're trying to do, but ViewData is a data construct that only has relevance in the Controller and when the View is being executed. By the time the JavaScript is running, it's in the context of a rendered page, so ViewData is meaningless. If, in your view you had

<input id='MyMessage' type='hidden' value='<%=(string)ViewData['Message'] %>' />,

you could then get the value you're looking for with

$('#MyMessage').val();

or

document.getElementById("MyMessage").value;
DaveDev
You can also use an object to build the parameters. `var formData = { FlashName: FlashName};`
Ryan
Thank you for assistance, I am still getting null value for the var, do I need to change the HomeController! from ViewData["Message"] = FlashName; return View(); }
@hnabih, I've updated the answer in response to your question. Not sure if it's what you're asking though..
DaveDev
Thanks Dave, I will post it with the script as another question because commen only accept less words than I need
@hnabih, if you find that this is the correct answer it's good StackOverflow practice to accept it as such. Thanks
DaveDev