Manoj Maharana Manoj Maharana - 3 months ago 9
Ajax Question

how can i pass a string variable in json by using GET method

When I receive a string value as an object then return false calls and output getting null in controller.

How to fix this?

How can I pass a string variable in json??

Here is the code:

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"


Here is the controller:

[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;
}
}

Answer

Change URL in the ajax call and remove the data parameter

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"
Comments