javascriptasp.netajaxasp.net-corejsonresult

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


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..


Solution

  • 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)
    {
    ...........
    }