2015-12-07 62 views
1

我的觀點包含這樣一種形式:與更新的div郵政的表單數據控制器

@model WebApplication1.Models.Books 

<form action="/Test/Run" method="POST"> 
    <table> 
     <tr> 
      <td> 
       Name: @Html.TextBoxFor(m => m.Title) 
      </td> 
     </tr> 
     <tr> 
      <td> 
       <input type="button" id="getbooksbutton" value="Get Books From Person"/> 
      </td> 
      <td> 
       <div id="books"></div> 
      </td> 
     </tr> 
     <tr> 
      <td> 
       <input type="submit" value="Send Data"/> 
      </td> 
     </tr> 
    </table> 
</form> 

我的模型:

public class Books 
{ 
    public string Title { get; set; } 
    public List<BooksDetails> BooksData { get; set; } 
} 

public class BooksDetails 
{ 
    public string BookVersion { get; set; } 
    public string BookPrice { get; set; } 
} 

我正在做一個ajax調用我的控制器getbooksbutton點擊參數「標題」,它返回並更新div「書籍」更多的詳細信息爲本書給出的名稱成功。

股利獲取我的AJAX功能這樣的更新:

var div = $("#books"); 
$.each(data.BooksData, function(i, item) { 
    div.append("<br/>" + "Book Version: " + item.BookVersion + ", Set your Price: " + "<input type=\"text\" name=\"item.BookPrice\" value=\"0\">"); 
}); 

用戶應該能夠設定一個價格爲每本書,這就是爲什麼我追加輸入字段時, div得到更新。

當用戶點擊「最終」提交按鈕時,我想通過更新的BooksData對象將圖書對象發佈到我的控制器。

我看了很多例子,但我沒有找到如何工作。

+0

將您的人物對象發佈代碼。 –

回答

0

必須使用模型綁定到一個列表,你可以改變你的代碼,這樣

div.append("<br/>" + "Book Title: " + item.BookTitle + ", Set your Price: " + "<input type=\"text\" name=\"[i].BookPrice\" value=\"0\">"); 

相關詳細信息,請參閱model binding to a list

+0

僅鏈接答案不是答案。請在答案中包含相關的詳細信息。 – CodeCaster

+0

@CodeCaster我將解決方案代碼添加到我的answer.but我把鏈接快速回答 –

+0

沒有人正在尋找快速答案,我們希望完整和正確的答案。 :) – CodeCaster

0

這是你可以用它來解決的方式你問題。這不是測試解決方案。

var Books = ""; 

$("#table tbody tr").each(function (index) { 
    $("td", this).each(function() { 
     if($(this).find('.books') != "" && $(this).find('.books')!=undefined) 
     { 
      var inputValues = []; 
      $('#books input').each(function() {    
       inputValues.push($(this).val());     
      }) 
      Books = { "Title" : "Your title", "BooksData" : inputValues };    
     } 
    }); 
}); 

現在使用Ajax調用張貼此Books對象到控制器。