2016-08-30 8 views
0

文字列値をオブジェクトとして受け取った場合、false呼び出しを返し、コントローラでnullを出力します。GETメソッドを使用してjsonで文字列変数を渡す方法

これを修正するにはどうすればよいですか?

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" 

はコントローラです:AJAX呼び出しで

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

これは「取得」リクエストですか? –

+0

はいsir..itはリクエストを受け取ります –

+1

クエリ文字列のパラメータを渡します。URL url:/ TLocation/EditLocation?LocationId = "+ obj' –

答えて

3

変更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" 
+0

[object HTMLInputElement]文字列値 –

+0

EditLocation関数にデバッグポイントを置き、 'LocationId'を取得しているかどうかを確認しますか? –

+0

どこにデバッガを置くべきですか?私はすでにデバッガを置いています –

0

お使いのコントローラ機能を書きますこのような。 LocationIdが適切なJSON構造を持っている場合リストはあなたに含まれるデータを表示します:

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

}

+0

これは何ですか?私は何をすべきですか? –

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

あなたは正確に私たちが問題を整理するのに役立ちますあなたのブラウザのデバッグウィンドウに表示されるエラーを、私たちに伝えることができます。

+0

エラーはここにあります:TLocation:764 Uncaught文字列がありませんでした。オブジェクトを持っていた............. if(typeof(obj)!== "string"){throw "文字列を持たず、" + typeof(obj); }この行で............... near + typeof(obj); .....私は何をすべきですか? –

+0

OKです。つまり、あなたのコードは文字列を期待していますが、それは関数に渡されたものではありません。 'obj'はHTML要素やjQueryオブジェクトのようではないので、それが何であるかを知る必要があります。私は元の投稿を編集し、 'EditRow'を呼び出すコードを追加する必要があると思います。これは機能に何が入っているのかを見るのに役立ちます。また、私はあなたが渡しているオブジェクトの種類を追跡するのに役立つ余分な行または2つで答えを更新します。 –

+0

上記のコードを実行したときにどのようなエラーが発生したかを教えてください。ちょっと変わったところです。 –

関連する問題