【问题标题】:how to assign a javascript value to a tempdata如何将javascript值分配给临时数据
【发布时间】:2013-03-03 21:31:55
【问题描述】:

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

        });

【问题讨论】:

  • 你不能这样做...
  • Js 在客户端运行,asp.net 在服务器端运行,还有问题吗?为什么需要它?
  • 您需要做的是向端点发出 ajax 请求以从您的 javascript 中保存变量。

标签: javascript asp.net-mvc asp.net-mvc-3


【解决方案1】:

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

$(".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 发布而不是获取和检查表单令牌以提高安全性,例如建议的 here

【讨论】:

    【解决方案2】:

    我尝试使用 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,我永远不会想到这个方向

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-15
      • 2012-09-02
      • 1970-01-01
      • 1970-01-01
      • 2015-07-06
      相关资源
      最近更新 更多