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'
                });
            }

        });
Moeez Agha
  • 217
  • 2
  • 4
  • 14

2 Answers2

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.

Community
  • 1
  • 1
gdp
  • 8,032
  • 10
  • 42
  • 63
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

Darkloki
  • 676
  • 1
  • 13
  • 31