3

i want to save java scrip var in to asp.net mvc Temp-data but it is giving syntax error

$(".PopReviewNo").click(function () {
            if (($('textarea').val().length == 0)) {
                $('.comm').addClass("layout");
            }
            else {
                $(".comm").removeClass("layout");
                var comment = $("#comme").val();
                **@TempData["CommentForPop"]= $("#comme").val();** ///Check this one 



                $.fancybox({
                    'transitionIn': 'elastic',
                    'transitionOut': 'elastic',
                    'easingIn': 'easeOutBack',
                    'easingOut': 'easeInBack',
                    'width': 850,
                    'height': 394,
                    href: "/Stores/PopReview/@Model.Company.id?comment=" + comment,
                    'type': 'iframe'
                });
            }

        });
3
  • 2
    you can not do this... Commented Mar 15, 2013 at 8:35
  • 1
    Js runs on a client side, asp.net on a server side, anymore questions? Why do you need it? Commented Mar 15, 2013 at 8:35
  • What you will have to do, is make a ajax request to an endpoint to save the variable from your javascript. Commented Mar 15, 2013 at 9:01

2 Answers 2

2

You can do this as an alternative, send the data to an endpoint for saving:

$(".PopReviewNo").click(function () {
        if (($('textarea').val().length == 0)) {
            $('.comm').addClass("layout");
        }
        else {
            $(".comm").removeClass("layout");
            var comment = $("#comme").val();

            var myVariableToSave = $("#comme").val(); 

            //Send the variable to be saved              
            $.getJSON('@Url.Action("myendpoint")', { dataToSave: myVariableToSave}, function(data) {
                 //show a message if you want
            });

            $.fancybox({
                'transitionIn': 'elastic',
                'transitionOut': 'elastic',
                'easingIn': 'easeOutBack',
                'easingOut': 'easeInBack',
                'width': 850,
                'height': 394,
                href: "/Stores/PopReview/@Model.Company.id?comment=" + comment,
                'type': 'iframe'
            });
        }

    });

Bare in mind TempData is meant for persistance between requests, therefore will get cleared at the end of the request. So look for some other storage for your variable to save.

public ActionResult MyEndPoint(string dataToSave)
{
    if(string.IsNullOrEmpty(dataToSave))
    {
         return Json(new { message = "Empty data to save"}, JsonRequestBehaviour.AllowGet);
    }

    //Save it to session or some other persistent medium
    Session["dataToSave"] = dataToSave;

    return Json(new { message = "Saved"}, JsonRequestBehaviour.AllowGet);
}

You can also perform a ajax post instead of a get and check form tokens for more security, like suggested here.

Sign up to request clarification or add additional context in comments.

Comments

1

I tried using gdp 's answer but kept getting "Illegal characters in path". Instead I modified it a little to use AJAX:

$(".PopReviewNo").click(function () {
    if (($('textarea').val().length == 0)) {
        $('.comm').addClass("layout");
    }
    else {
        $(".comm").removeClass("layout");
        var comment = $("#comme").val();

        var myVariableToSave = $("#comme").val(); 


          $.ajax({
          // alert(myVariableToSave); // Check the value.
          type: 'POST',
          url: '/mycontroller/myendpoint',
          data: "dataToSave=" + myVariableToSave,
          success: function (result) {
        //show a message if you want
            },
         error: function (err, result) {
         alert("Error in assigning dataToSave" + err.responseText);
          }


        $.fancybox({
            'transitionIn': 'elastic',
            'transitionOut': 'elastic',
            'easingIn': 'easeOutBack',
            'easingOut': 'easeInBack',
            'width': 850,
            'height': 394,
            href: "/Stores/PopReview/@Model.Company.id?comment=" + comment,
            'type': 'iframe'
        });
    }

});

and my action:

    public ActionResult myendpoint(string dataToSave)  
    {
        if (string.IsNullOrEmpty(dataToSave))
        {
            return Json(new { message = "Empty data to save" },  JsonRequestBehavior.AllowGet);
        }

        //Save it to session or some other persistent medium
          ...

        //return Json(new { message = "Success" }, JsonRequestBehavior.AllowGet);
    // or
        return new EmptyResult();
    }

I take no credit for this, I would never had thought in this direction were it not for @gdp

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.