如何通过使用GET方法在json中传递字符串变量

时间:2016-08-30 06:29:21

标签: c# json ajax

当我收到一个字符串值作为对象时,返回false调用并在控制器中输出null。

如何解决这个问题?

如何在json中传递字符串变量?

以下是代码:

function EditRow(obj) {
 debugger
 //var jSon = JSON.stringify(obj);
 //data: { "LocationId": obj },
 $.ajax({
  url: "/TLocation/EditLocation/",
  type: "GET",
  cache: true,
  //async: true,
  //data: JSON.stringify({ LocationId: obj }),
  //data: { "LocationId": obj },
  data: {
   LocationId: JSON.stringify(obj)
  },
  success: function(result) {
   $("#EditLocation").html(result);
  },
  error: function(result) {
   alert('');
  }
 });
 return false;
}
obj = "Test"

这是控制器:

[System.Web.Services.WebMethod]
        public ActionResult EditLocation(string LocationId)
        {
            try
            {
                if (Session["Type"] == null)
                    return RedirectToAction("Index", "Account");
                var model = new TLocationModel();

                LocationBL objloc = new LocationBL();
                //model.IsEmailDublicate = "0";

                if (LocationId.Length > 0)
                {
                    var Item = objloc.getLocationById(LocationId);
                    if (Item != null)
                    {
                        model.LocationID = Item.LocationID != null ? Item.LocationID : "";
                        model.Description = Item.Description != null ? Item.Description : "";
                        model.Category = Item.Category != null ? Item.Category : "";
                        model.Aisle = Item.Aisle != null ? Item.Aisle : "";
                        model.Self = Item.Shelf != null ? Item.Shelf : "";
                        model.Bin = Item.Bin != null ? Item.Bin : "";
                        model.PrintBarcode = Item.PrintBarcode != null ? Item.PrintBarcode.Value : false;
                    }

                }
                return PartialView("EditLocation", model);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

3 个答案:

答案 0 :(得分:3)

更改ajax调用中的URL并删除数据参数

function EditRow(obj) {
debugger
//var jSon = JSON.stringify(obj);
//data: { "LocationId": obj },
$.ajax({
 url: "/TLocation/EditLocation/LocationId=" + obj,
 type: "GET",
 cache: true,
 //async: true,
 //data: JSON.stringify({ LocationId: obj }),
 //data: { "LocationId": obj },
 success: function(result) {
 $("#EditLocation").html(result);
 },
 error: function(result) {
 alert('');
}
});
return false;
}
obj = "Test"

答案 1 :(得分:0)

像这样写你的控制器功能。如果LocationId具有正确的JSON结构, values 列表将显示包含的数据:

public ActionResult EditLocation(string LocationId)
{
try
{
    if (Session["Type"] == null)
        return RedirectToAction("Index", "Account");
    var model = new TLocationModel();

    LocationBL objloc = new LocationBL();
    //model.IsEmailDublicate = "0";

    JObject JLocationId = JObject.Parse(LocationId);
    IEnumerable<object> values = JLocationId.Values();
    foreach (var x in values)
    {
        //Console.WriteLine(x) or something like that to check the values of the JSON object
    }

    if (LocationId.Count > 0)
    {
        var Item = objloc.getLocationById(LocationId);
        if (Item != null)
        {
            model.LocationID = Item.LocationID != null ? Item.LocationID : "";
            model.Description = Item.Description != null ? Item.Description : "";
            model.Category = Item.Category != null ? Item.Category : "";
            model.Aisle = Item.Aisle != null ? Item.Aisle : "";
            model.Self = Item.Shelf != null ? Item.Shelf : "";
            model.Bin = Item.Bin != null ? Item.Bin : "";
            model.PrintBarcode = Item.PrintBarcode != null ? Item.PrintBarcode.Value : false;
        }

    }
    return PartialView("EditLocation", model);
}
catch (Exception ex)
{
    throw ex;
}

}

答案 2 :(得分:0)

这是我尝试过的。它已经完成了检查,以确保它获得正确的输入,因此如果它解决了问题,请检查。否则,它可能会帮助您追踪问题;

function EditRow(obj) {

    // expect 'obj' to be a string, since this is what is declared in the C# method
    if (obj === undefined) { throw "obj undefined"; }
    if (obj === null ) { throw "obj null"; }
    if (obj instanceof jQuery) { throw "Got a JQuery object"; }
    if (obj instanceof Element) { throw "Got an HTML element"; }

    // NEW EDIT
    if (typeof(obj) !== "string") { 
         throw "Did not have a string; had a " + typeof(obj) + " like " + JSON.stringify(obj, null, 4); 
    }

    console.log("Looks like a string of length " + obj.length, obj);

    var url = "/TLocation/EditLocation/LocationId=" + obj;

    console.log("URL is " + url);

    $.ajax({
        url: url,
        type: "GET",
        cache: true,
        success: function(result) {
            $("#EditLocation").html(result);
        },
        error: function(result) {
            alert('');
        }
    });

    return false;
}

如果您可以准确地告诉我们您在浏览器的调试窗口中看到的错误,那么这将有助于我们解决问题。