3

我想将 java scrip var 保存到 asp.net mvc Temp-data 但它给出了语法错误

$(".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'
                });
            }

        });
4

2 回答 2

2

您可以这样做,将数据发送到端点进行保存:

$(".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'
            });
        }

    });

请记住 TempData 用于请求之间的持久性,因此将在请求结束时被清除。因此,为您的变量寻找一些其他存储空间来保存。

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

您还可以执行 ajax 发布而不是获取和检查表单令牌,以提高安全性,就像这里建议的那样。

于 2013-03-15T09:08:33.250 回答
1

我尝试使用 gdp 的答案,但不断收到“路径中的非法字符”。相反,我对其进行了一些修改以使用 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'
        });
    }

});

和我的行动:

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

我对此不以为然,如果不是@gdp,我永远不会想到这个方向

于 2016-07-08T19:51:01.667 回答