无法发送参数 @ViewData[“Param”]

huangapple go评论65阅读模式
英文:

Can't send parameter with @ViewData["Param"]

问题

我需要帮助,无法将 ViewData 中的参数从 JavaScript 发送到控制器,可能是因为控制器使用 JsonResult,但我到目前为止还没有找到解决方案,是否有修复的方法?

这是我的 JavaScript 代码:

var urlString2 = "@Url.Action("GetAttributeLabel", "MasterDataTemp")?modifierId=@ViewData["ModifierId"]";
$.ajax({
   url: urlString2,
   type: "GET",
   dataType: "json",
   contentType: 'application/json; charset=utf-8',
   async: true,
   processData: false,
   cache: false,
   success: function (data) {
      $.each(data, function (i, item) {
         dataAttributeLabel.push(item.AttributeName);
      });
   },
   error: function (xhr) {
      alert('error');
   }
});

这是我的控制器代码:

public JsonResult GetAttributeLabel(Guid modifierId)
{
   ...........
}

参数始终发送空的 GUID,请帮忙。

英文:

I need help, i can't send parameter from ViewData in javascript to Controller, maybe because in controller using JsonResult, but I haven't found the solution till now, is there any way to fix it ?

this my js :

var urlString2 = "@Url.Action("GetAttributeLabel", "MasterDataTemp")?modifierId=@ViewData["ModifierId"]";
$.ajax({
   url: urlString2,
   type: "GET",
   dataType: "json",
   contentType: 'application/json; charset=utf-8',
        async: true,
        processData: false,
        cache: false,
        success: function (data) {
     $.each(data, function (i, item) {
        dataAttributeLabel.push(item.AttributeName);
    });
   },
    error: function (xhr) {
       alert('error');
   }
});

this is my controller :

public JsonResult GetAttributeLabel(Guid modifierId)
{
...........
}

The parameter always send empty guid, please help..

答案1

得分: 0

请尝试以下方式:

@{
    string guid = ViewData["ModifierId"].ToString();
}
var urlString2 = "MasterDataTemp/GetAttributeLabel?modifierId=" + guid;

$.ajax({
    url: urlString2,
    type: "GET",
    dataType: "json",
    success: function (data) {
        $.each(data, function (i, item) {
            dataAttributeLabel.push(item.AttributeName);
        });
    },
    error: function (xhr) {
        alert('error');
    }
});

在控制器中:

public JsonResult GetAttributeLabel(string modifierId)
{
    // 这里是你的控制器逻辑代码
}
英文:

Please try like below

@{
 string guid = ViewData["ModifierId"].ToString();
}
var urlString2 = "MasterDataTemp/GetAttributeLabel?modifierId="+"@guid";

$.ajax({
   url: urlString2,
   type: "GET",
   dataType: "json",
   success: function (data) {
     $.each(data, function (i, item) {
        dataAttributeLabel.push(item.AttributeName);
    });
   },
    error: function (xhr) {
       alert('error');
   }
});

And at controller

public JsonResult GetAttributeLabel(string modifierId)
{
...........
}

答案2

得分: 0

首先,如果您希望发送 JSON 数据,应避免使用 jQuery Ajax 的 GET 请求,而可以尝试使用 POST 请求。

其次,您可以在 @Section 中生成URL,如下所示:

@{
    string val = ViewData["key"] != null ? ViewData["key"].ToString() : "defaultval";
    
    var routedic = new Dictionary<string, string>()
    {
        { "key", val }
    };

    var url = Url.Action("Test", "Home", routedic);
}

然后发送请求:

<script>
  var urlstr = "@(url)";
  // 在控制台中检查URI
  console.log(urlstr);
  $.ajax({
    url: urlstr,
    type: "GET"
  });
</script>

现在可以正常工作。

英文:

Firstly, you have to avoid sending JSON data with jQuery Ajax GET request, if you do want to send JSON data, try with a POST request.

Secondly, you could generate the url in @Section like this:

@{&#160;&#160;&#160;&#160; &#160;&#160;&#160;
    string val = ViewData[&quot;key&quot;] != null ? ViewData[&quot;key&quot;].ToString()
                                         : &quot;defaultval&quot;;&#160;
    &#160;&#160;&#160;
    var routedic = new Dictionary&lt;string, string&gt;()
                   &#160;&#160;&#160;&#160;{
                           { &quot;key&quot;, val }
                   &#160;&#160;&#160;&#160;};&#160;
    &#160;&#160;&#160;
    var url = Url.Action(&quot;Test&quot;, &quot;Home&quot;, routedic);
} 

And then send the request:

&lt;script&gt;&#160;&#160;&#160;&#160;
  var urlstr=&quot;@(url)&quot;;
  // check the uri in console&#160;&#160;
&#160;&#160;console.log(urlstr);&#160;&#160;&#160;
&#160;$.ajax({&#160;&#160;&#160;&#160;&#160;&#160;&#160;
&#160;  url:urlstr,&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;
   type:&quot;GET&quot;&#160;&#160;&#160;&#160;
});
&lt;/script&gt;

It works well now:

无法发送参数 @ViewData[“Param”]

huangapple
  • 本文由 发表于 2023年2月16日 10:46:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75467336.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定