2012-11-21 32 views
0

我在我看來如何獲得dropdowlistfor一個表裏面是使用JSON

<table class="tbl" id="tbl"> 
     <thead> 
      <tr> 
       <th> 
        Region 
       </th> 
       <th> 
        Owner 
       </th> 
      </tr> 
     </thead> 
     <tbody> 
      @if (Model != null) 
      { 
       foreach (var item in Model.Regions) 
       { 
       <tr> 
        <td> 
         @Html.DisplayTextFor(i => item.Name) 
        </td> 
        <td> 
         @Html.DropDownListFor(i => item.Users, new SelectList(item.Users, "Id", "Name")) 
        </td> 
       </tr> 
      } 
      } 
     </tbody> 

有此表的所選項目的價值,我有此JSON代碼來讀它

//This function is used for sending data(JSON Data) to SalesController 
function TestFieldSave() { 
    // Step 1: Read View Data and Create JSON Object 

    var processModel = { "Name": "", "Description": "", "Code": "", "Regions": []}; 

    var regionModel = { "Name": "", "Users": []}; 

    var userModel = { "Id": "", "Name": ""}; 

    var oTable = $('.tbl').dataTable().fnGetData(); 

    for (var i = 0; i < oTable.length; i++) { 

     regionModel.Name = oTable[i][0]; 
     userModel.Id  = oTable[i][1]; 

     regionModel.Users.push(userModel); 

     processModel.Regions.push(regionModel); 

     userModel = { "Id": "", "Name": ""}; 

     regionModel = { "Name": "", "Users": []}; 
    } 
    // Step 1: Ends Here 

    // Set 2: Ajax Post 
    // Here i have used ajax post for saving/updating information 
    $.ajax({ 
     url: '/Process/Create', 
     data: JSON.stringify(processModel), 
     type: 'POST', 
     contentType: 'application/json;', 
     dataType: 'json', 
     success: function (result) { 

      if (result.Success == "1") { 
       window.location.href = "/Process/Index"; 
      } 
      else { 
       alert(result.ex); 
      } 
     } 
    }); 
} 

這些都是我的模型

namespace TestingTool.ViewModels 
{ 
    public partial class ProcessModel 
    { 
     public string Name { get; set; } 
     public string Description { get; set; } 
     public string Code { get; set; } 

     public virtual ICollection<RegionModel> Regions { get; set; } 
    } 
} 

namespace TestingTool.ViewModels 
{ 
    public class RegionModel 
    { 
     public int Id { get; set; } 
     public string Name { get; set; } 
     public virtual ICollection<UserModel> Users { get; set; } 

    } 
} 

namespace TestingTool.ViewModels 
{ 
    public class UserModel 
    { 
     public int Id { get; set; } 
     public string Name { get; set; } 
    } 
} 

我無法從DropDownListFor讀取。我該如何解決這個問題?

+0

哪裏是你表現出從示例代碼來的'oTable'變量?它是如何被實例化的? –

+0

var oTable = $('。tbl')。dataTable()。fnGetData(); –

回答