2016-04-24 7 views
1

GeneralDictViewmodelオブジェクトとその辞書を取得するリクエストを送信しますが、AJAXレスポンスの辞書ではなく、それ自身を取得します。これをどうすれば解決できますか?AJAXレスポンスのMVCモデルを読む

$.ajax({ 
     url: getAllGeneralDictUrl, 
     type: 'POST', 
     cache: false, 
     dataType: 'json', 
     contentType: 'application/json; charset=utf-8', 
     success: function (data, status, resObject) { 
      alert("success..." + data); 

     }, 
     error: function (xhr) { 
      alert(xhr.responseText); 
     } 
    }); 


    public JsonResult GetAllGeneralDict() 
    { 
      GeneralDictServiceClient generalDictSvc = new GeneralDictServiceClient(); 
      GeneralDictViewModel generalDictRes = new GeneralDictViewModel(); 
      generalDictRes.ShipTypes = generalDictSvc.GetGeneralDict("SHIP_TYPE").ToDictionary(x => x.KEY_ITEM, x => x.VALUE_ITEM); 
      generalDictRes.BodyMaterials = generalDictSvc.GetGeneralDict("BODY_MATERIAL").ToDictionary(x => x.KEY_ITEM, x => x.VALUE_ITEM); 
      generalDictRes.Designations = generalDictSvc.GetGeneralDict("DESIGNATION").ToDictionary(x => x.KEY_ITEM, x => x.VALUE_ITEM); 
      return Json(generalDictRes); 
    } 
+0

であなたのリターン機能でJSONデータをJsonRequestBehavior.AllowGetを書くべきことはあなたのURLに問題があるようです。 url:getAllGeneralDictUrl、 –

+0

の代わりにurl:getAllGeneralDictを使用しますが、リクエストを送信します。私はそれをデバッグすることができます – HesenliEldar

+0

私はリターン(新しい文字列( "何か"))を送信するが、それは動作しますが、モデルでは動作しません。辞書であるモデルのプロパティに問題があるのは可能ですか? – HesenliEldar

答えて

0

まず第一に、あなたがデータをフェッチしている場合は、あなたのAJAX呼び出しに「GET」必要があるはずです。

try { 
    $.ajax({ 
     url: 'template/GetAllGeneralDict', 
     type: 'POST', 
     cache: false, 
     dataType: 'json', 
     contentType: 'application/json; charset=utf-8', 
     success: function (data, status, resObject) { 
      alert("success..." + data); 

     }, 
     error: function (xhr) { 
      alert(xhr.responseText); 
     } 
    }); 
} catch (e) { 
    window.location.href = SiteRootPath + "default.aspx"; 
    $('#loading').hide(); 
} 

第二:あなたはコントローラ

public JsonResult GetAllGeneralDict() 
     { 
      var abcObj = new Animal() { Id = 1, Name = "ZTom" }; 

      return Json(abcObj, JsonRequestBehavior.AllowGet); 
     }