英文:
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:
@{        
string val = ViewData["key"] != null ? ViewData["key"].ToString()
: "defaultval"; 
   
var routedic = new Dictionary<string, string>()
    {
{ "key", val }
    }; 
   
var url = Url.Action("Test", "Home", routedic);
}
And then send the request:
<script>    
var urlstr="@(url)";
// check the uri in console  
  console.log(urlstr);   
 $.ajax({       
  url:urlstr,        
type:"GET"    
});
</script>
It works well now:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论