2012-04-13 9 views
3

私は誰でも助けてくれることを願っています(私の英語はすみません)。 ajaxで配列のun配列を送信したいときに問題があります。 私のモデルは次のとおりです。jsonオブジェクトの配列をasp.net mvcのajaxでアクションに送信する3

public class SJSonModel 
{ 
    public string Name { get; set; } 
    public bool isChecked { get; set; }  
} 

public class SJSonModelList 
{ 
    public List<SJSonModel> Features { get; set; } 
    public List<SJSonModel> MenuItems { get; set; } 
} 

コントローラ:

[HttpPost] 
    public ActionResult CheckPreferences(SJSonModelList postData) 
    { 
     BindUserFeatures(postData.Features); 

     return Json(new { status = "Success", message = "Passed" }); 
    } 

簡略図:

<div class="Feature borderRadius Items"> 
    <h2>Title 
     <input type="checkbox" class="Item" name="featureName"/> 
    </h2> 

    <div class="FeatureDetails subItems">     
     <a href="@Url…">featureName</a> 
     <input type="checkbox" class="subItem" name="subItemName"/> 
    </div> <!-- endOf FeatureDetails --> 

のjQueryコード:

var isChecked = false; 
    var features = new Array(); 
    var menuItems = new Array(); 
    var postData = new Array(); 

は、ここで私は機能を埋める、各機能のFEATURENAME/menuItemNameとにisCheckedブール/ menuItemに

menuItems.push({ "Name": $(this).attr('name'), "isChecked": isChecked }); 
features.push({ "Name": $(this).attr('name'), "isChecked": isChecked }); 

postData.push({ "features": features, "menuItems": menuItems }); 
postData = JSON.stringify(postData); 

とメニューアイテムのAjax機能:

$(':submit').click(function() { 

     postData.push({ "features": features, "menuItems": menuItems }); 
     postData = JSON.stringify(postData); 

     $.ajax({ 
       url: '@Url.Action("CheckPreferences")', 
       type: 'POST', 
       data: postData, 
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       traditional: true, 
       success: function() { window.alert('@Resource.AjaxSuccess'); }, 
       error: function (event, request, settings) { window.alert('@Resource.AjaxError' + ' : ' + settings); }, 
       timeout: 20000 
     }); //endOf $.ajax 
    }); //endOf :submit.click function 

私は(POSTデータ)の警告を行う場合には、クライアント側では、各項目の真の値を含んでいますが、コネクター内ではpostData.FeaturesとpostData.MenuItemsはnullです。

私はあまりにもコントローラに一つだけのアレイ合格することを試みた:コントローラーに

{… data: features,…} 

:$アヤックスで

features = JSON.stringify(features); 

ActionResult CheckPreferences(IEnumerable<SJSonModel> features) 

、それが正常に動作しますしかし、私はjsonオブジェクトの配列を私のcontollerに渡す方法を知らない。だから私はここで答えを得ることを願っています:)

ありがとうございます。

+0

があり、とにかくあなたが実際にサーバーに起こっているJSONオブジェクトを投稿できます?それが右に押されていない可能性があります。フィドラーを試して、アヤックスのリクエストが – Qpirate

答えて

11

代わりに別の配列にあなたの配列を組み合わせることで、個々のアクションメソッドへのパラメータ、のようなものとして、あなたがそれらを送信する最良のだ:

var features = new Array(); 
var menuItems = new Array(); 
menuItems.push({ "Name": $(this).attr('name'), "isChecked": isChecked }); 
features.push({ "Name": $(this).attr('name'), "isChecked": isChecked }); 

は、我々はまだあなたの二つの配列を持っていると仮定次に、あなたはJQueryのAjaxの呼び出しで、次の操作を行います。

$.ajax({ 
     url: '@Url.Action("CheckPreferences")', 
     type: 'POST', 
     datatype: "json", 
     traditional: true, 
     data: { 
      menuItems: JSON.stringify(menuItems), 
      features: JSON.stringify(features) 
     }, 
     success: function() { window.alert('@Resource.AjaxSuccess'); }, 
     error: function (event, request, settings) { 
      window.alert('@Resource.AjaxError' + ' : ' + settings); }, 
     timeout: 20000 
}); 

その後、あなたのコントローラのメソッドは次のようになります。

[HttpPost] 
public ActionResult CheckPreferences(string menuItems, string features) 
{ 
    var js = new JavaScriptSerializer(); 
    var deserializedMenuItems = (object[])js.DeserializeObject(menuItems); 
    var deserializedFeatures = (object[])js.DeserializeObject(features); 
    var myFeatures = new List<SJSonModel>(); 
    var myMenuItems = new List<SJSonModel>(); 

    if (deserializedFeatures != null) 
    { 
     foreach (Dictionary<string, object> newFeature in deserializedFeatures) 
     { 
      myFeatures.Add(new SJSonModel(newFeature)); 
     } 
    } 

    if (deserializedMenuItems != null) 
    { 
     foreach (Dictionary<string, object> newMenuItem in deserializedMenuItems) 
     { 
      myMenuItems.Add(new SJSonModel(newMenuItem)); 
     } 
    } 

    var myModelList = new SJSonModelList(myFeatures, myMenuItems); 

    return Json(""); 

私もそうのように、上記のコードで動作するようにコンストラクタに置くことによって、あなたのクラスを編集した:

public class SJSonModel 
{ 
    public SJSonModel(Dictionary<string, object> newFeature) 
    { 
     if (newFeature.ContainsKey("Name")) 
     { 
      Name = (string)newFeature["Name"]; 
     } 
     if (newFeature.ContainsKey("isChecked")) 
     { 
      isChecked = bool.Parse((string)newFeature["isChecked"]); 
     } 
    } 

    public string Name { get; set; } 
    public bool isChecked { get; set; } 
} 

public class SJSonModelList 
{ 
    public SJSonModelList(List<SJSonModel> features, List<SJSonModel> menuItems) 
    { 
     Features = features; 
     MenuItems = menuItems; 
    } 

    public List<SJSonModel> Features { get; set; } 
    public List<SJSonModel> MenuItems { get; set; } 
} 
+0

こんにちは、ありがとうあなたのメッセージのために。私はまだそれを動作していない、menuItemsと機能の値はnullです –

+0

こんにちは@LilitMR、私の答えの私の新しい編集を確認してください。私はその正確なコードをテストし、うまく動作します:) – mattytommo

+0

こんにちは、あなたのメッセージのために多くのありがとう。私はこの午前中にオフィスで試してみるでしょう。私はあなたにすばやく答えます。罰金WEを渡してください:) –

1

クライアントからシリアル化されたデータを渡すので、postDataをタイプ文字列としてコントローラに定義し、JavascriptSerializer.Deserializeを使用してJSONをオブジェクトに逆シリアル化します。この方法で、デシリアライズ中に発生する可能性のあるエラーをキャッチし、送信されたJSONをデバッグすることもできます。デシリアライザが大文字と小文字の区別のためにフィールド名を一致させることを期待しているかどうかは分かりません。モデルでは配列を「フィーチャ」と「MenuItems」として定義し、Javascriptでは配列を「フィーチャ」と「menuItems」として定義します。

+0

のidemであることを確認してください。それは動作しません。 –

+0

正確には機能しませんでしたか?アプリケーションをデバッグすると、コントローラに返される文字列にJSONが表示されますか?それとも、デシリアライゼーションで失敗するのでしょうか? –

+0

こんにちはKevin、あなたのメッセージをありがとう。私はちょうどシリアル化を正しく使用していませんでしたが、今は問題なく動作します:) –

関連する問題