2010-06-14 76 views
0

我有以下代碼和測試值在控制器後總是空。什麼是錯用下面的代碼:後在控制器測試爲空

型號:

public class Suitcase 
{ 
    public string Color { get; set; } 
    public string[] Size { get; set; } 
    public List<string> Clothes { get; set; } 
    public List<Test> test { get; set; } 
} 
public class Test 
{ 
    public string Name { get; set; } 
    public int ID { get; set; } 
} 

的觀點:

<fieldset> 
     <legend>All about my baggage</legend> 

     <div class="editor-label"> 
      <%: Html.LabelFor(model => model.Color) %> 
     </div> 
     <div class="editor-field"> 
      <%: Html.TextBoxFor(model => model.Color) %> 
     </div> 
     <br /> 

     <div class="editor-label"> 
      Width, Height, Depth: 
     </div> 
     <div class="editor-field"> 
      ml.TextBoxFor(model => model.Depth, new { style = "width:50px;" })%> 
      </div> 
     <br /> 

     <div class="editor-label">Suitcase Contents</div> 
     <div class="editor-field"> 

      <div id="clothes-editor"> 
       Clothing Item: <input type="text" id="new-clothes-item" style="width:150px" /> <button id="add-clothes">Add to suitcase</button>     
      </div> 

      <b>Items currently in suitcase:</b> 
      <ul id="clothes-list">      
      </ul> 

     </div> 
     <p> 
      <button id="pack-it">Put on Baggage Carosel</button> 
     </p> 



    </fieldset> 

    <script type="text/javascript" language="javascript"> 
     $(function() { 
      $("button").button(); 

      // allow users to add items to the suitcase 
      $("#add-clothes").click(function() { 
       var clothesText = $("#new-clothes-item"); 
       $("#clothes-list").append("<li>" + clothesText.val() + "</li>"); 
       clothesText.val("").focus(); 
      }); 

      // pack the suitcase up and send it to the baggage carosel...erm...controller 
      $("#pack-it").click(function() { 

       var clothesList = []; 

       $("#clothes-list li").each(function() { 
        clothesList.push($(this).text()) 
       }); 

       var SizeList = []; 
       SizeList[0] = "Medium"; 
       SizeList[1] = "Large"; 
       SizeList[2] = "small"; 

       var Data = new Object(); 
       Data.test = []; 

       var reading = {}; 
       reading.Name = "Micheal" 
       reading.ID = 123; 
       Data.test[0] = reading; 

       reading.Name = "Rowen" 
       reading.ID = 1234; 
       Data.test[1] = reading; 



       $.ajax({ 
        type: 'POST', 
        traditional: true, 
        data: { 
         Color: $("#Color").val(), 
         Size: SizeList, 
         Clothes: clothesList, 
         test: Data.test 
        } 
       }); 
      }); 
     });   
    </script> 

控制器:

[HttpPost] 
    public EmptyResult Suitcase(Suitcase lookWhatIPacked) 
    { 
     return new EmptyResult(); 
    } 
+0

你能檢查螢火蟲什麼值真的張貼? – 2010-06-14 11:38:19

+0

儘管Data.test包含值,我有對象{測試=},爲什麼這樣呢? – learning 2010-06-14 12:02:17

回答

0

它可能沒有關係,但我不認爲這段代碼正在做你想做的事情:

var reading = {}; 
reading.Name = "Micheal" 
reading.ID = 123; 
Data.test[0] = reading; 

reading.Name = "Rowen" 
reading.ID = 1234; 
Data.test[1] = reading; 

這將在同一個對象Data.test兩次,因爲你不設置reading是一個新的數組,所以你要更新原來的對象有「羅文」爲Name和1234作爲ID