如何将JavaScript数值分配给TempData

3
我想将JavaScript变量保存到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'
                });
            }

        });

2
你不能这样做... - AliRıza Adıyahşi
1
Js 运行在客户端,asp.net 运行在服务器端,还有其他问题吗?你为什么需要它? - Sergio
你需要做的是,通过ajax请求将javascript中的变量保存到一个端点。 - gdp
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 Post而不是Get,并检查表单令牌以获得更多安全性,就像这里建议的那样。


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 的启发,我从未想过这个方向。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接