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

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

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

问题

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

这是我的 JavaScript 代码:

  1. var urlString2 = "@Url.Action("GetAttributeLabel", "MasterDataTemp")?modifierId=@ViewData["ModifierId"]";
  2. $.ajax({
  3. url: urlString2,
  4. type: "GET",
  5. dataType: "json",
  6. contentType: 'application/json; charset=utf-8',
  7. async: true,
  8. processData: false,
  9. cache: false,
  10. success: function (data) {
  11. $.each(data, function (i, item) {
  12. dataAttributeLabel.push(item.AttributeName);
  13. });
  14. },
  15. error: function (xhr) {
  16. alert('error');
  17. }
  18. });

这是我的控制器代码:

  1. public JsonResult GetAttributeLabel(Guid modifierId)
  2. {
  3. ...........
  4. }

参数始终发送空的 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 :

  1. var urlString2 = "@Url.Action("GetAttributeLabel", "MasterDataTemp")?modifierId=@ViewData["ModifierId"]";
  2. $.ajax({
  3. url: urlString2,
  4. type: "GET",
  5. dataType: "json",
  6. contentType: 'application/json; charset=utf-8',
  7. async: true,
  8. processData: false,
  9. cache: false,
  10. success: function (data) {
  11. $.each(data, function (i, item) {
  12. dataAttributeLabel.push(item.AttributeName);
  13. });
  14. },
  15. error: function (xhr) {
  16. alert('error');
  17. }
  18. });

this is my controller :

  1. public JsonResult GetAttributeLabel(Guid modifierId)
  2. {
  3. ...........
  4. }

The parameter always send empty guid, please help..

答案1

得分: 0

请尝试以下方式:

  1. @{
  2. string guid = ViewData["ModifierId"].ToString();
  3. }
  4. var urlString2 = "MasterDataTemp/GetAttributeLabel?modifierId=" + guid;
  5. $.ajax({
  6. url: urlString2,
  7. type: "GET",
  8. dataType: "json",
  9. success: function (data) {
  10. $.each(data, function (i, item) {
  11. dataAttributeLabel.push(item.AttributeName);
  12. });
  13. },
  14. error: function (xhr) {
  15. alert('error');
  16. }
  17. });

在控制器中:

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

Please try like below

  1. @{
  2. string guid = ViewData["ModifierId"].ToString();
  3. }
  4. var urlString2 = "MasterDataTemp/GetAttributeLabel?modifierId="+"@guid";
  5. $.ajax({
  6. url: urlString2,
  7. type: "GET",
  8. dataType: "json",
  9. success: function (data) {
  10. $.each(data, function (i, item) {
  11. dataAttributeLabel.push(item.AttributeName);
  12. });
  13. },
  14. error: function (xhr) {
  15. alert('error');
  16. }
  17. });

And at controller

  1. public JsonResult GetAttributeLabel(string modifierId)
  2. {
  3. ...........
  4. }

答案2

得分: 0

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

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

  1. @{
  2. string val = ViewData["key"] != null ? ViewData["key"].ToString() : "defaultval";
  3. var routedic = new Dictionary<string, string>()
  4. {
  5. { "key", val }
  6. };
  7. var url = Url.Action("Test", "Home", routedic);
  8. }

然后发送请求:

  1. <script>
  2. var urlstr = "@(url)";
  3. // 在控制台中检查URI
  4. console.log(urlstr);
  5. $.ajax({
  6. url: urlstr,
  7. type: "GET"
  8. });
  9. </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:

  1. @{&#160;&#160;&#160;&#160; &#160;&#160;&#160;
  2. string val = ViewData[&quot;key&quot;] != null ? ViewData[&quot;key&quot;].ToString()
  3. : &quot;defaultval&quot;;&#160;
  4. &#160;&#160;&#160;
  5. var routedic = new Dictionary&lt;string, string&gt;()
  6. &#160;&#160;&#160;&#160;{
  7. { &quot;key&quot;, val }
  8. &#160;&#160;&#160;&#160;};&#160;
  9. &#160;&#160;&#160;
  10. var url = Url.Action(&quot;Test&quot;, &quot;Home&quot;, routedic);
  11. }

And then send the request:

  1. &lt;script&gt;&#160;&#160;&#160;&#160;
  2. var urlstr=&quot;@(url)&quot;;
  3. // check the uri in console&#160;&#160;
  4. &#160;&#160;console.log(urlstr);&#160;&#160;&#160;
  5. &#160;$.ajax({&#160;&#160;&#160;&#160;&#160;&#160;&#160;
  6. &#160; url:urlstr,&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;
  7. type:&quot;GET&quot;&#160;&#160;&#160;&#160;
  8. });
  9. &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:

确定