2017-03-06 66 views
1

我已經使用devextreme datagrid。我如何發送數據網格控制器在asp.net mvc? 我有我的觀點試過這樣:如何將數據網格devextreme發佈到控制器在asp.net mvc

@using (Html.BeginForm("action-name", "controller-name", FormMethod.Post)) 
      { 
       <div id="frm"></div> 
       <div id="grid"></div> 
       <div class="submit-button" id="submitBtn"></div> 
      } 

,這是我的模型:

public class class-name 
    { 
     public int id { get; set; } 
     public string Name { get; set; } 
     public string FName { get; set; } 
     public ContactInfo ContactInfo { get; set; } 
    } 

public class ContactInfo 
    { 
     public string type { get; set; } 
     public string value { get; set; } 
    } 

在我的位指示讓我的表單數據(類名),但網格數據(的ContactInfo)是null

+0

您是否在網格中使用編輯?你能提供你的數據網格選項嗎?還有一件重要的事情,你使用什麼DevExtreme版本? – Sergey

+0

我使用16.2版本。是的,我添加,更新和刪除行。但是當我點擊按鈕時,如何將datagrid中的所有數據發送到我的控制器,就像當我單擊提交按鈕並將表單數據發送到我的控制器時一樣。 – leman17

回答

2

在你的情況下,你可以使用ASP.NET MVC Wrappers for DevExtreme。在這種情況下,你將能夠結合這樣的網格的WebAPI控制器:

@(Html.DevExtreme().DataGrid() 
    .DataSource(ds => ds 
     .WebApi() 
     .Controller("GridData") 
     .Key("OrderID") 
     .LoadAction("GetAllOrders") 
     .InsertAction("InsertOrder") 
     .UpdateAction("UpdateOrder") 
     .DeleteAction("RemoveOrder") 
    ) 
) 

而且控制器:

public class DataGridWebApiController : ApiController { 

    [HttpGet] 
    public HttpResponseMessage GetAllOrders(DataSourceLoadOptions loadOptions) { 
     //... 
    } 

    [HttpPost] 
    public HttpResponseMessage InsertOrder(FormDataCollection form) { 
     //... 
    } 

    [HttpPut] 
    public HttpResponseMessage UpdateOrder(FormDataCollection form) { 
     //... 
    } 

    [HttpDelete] 
    public void RemoveOrder(FormDataCollection form) { 
     //... 
    } 
} 

嗯,這是沒有必要所有的數據網格的數據發送到服務器側。只管理更新的數據。

this article中有關於數據綁定的附加信息。

看看this demo以及。

+0

多數民衆贊成在偉大的!但我怎麼能發佈數據(包括我的表單和網格數據)到我的控制器,當我點擊提交菜單。我試過onclick方法按鈕,並在其中使用Ajax,但我沒有成功。 – leman17

+1

爲什麼要將所有數據網格發佈到服務器端?這聽起來有點奇怪。您可以使用內置的dxDataGrid功能處理數據更改。我的意思是爲CRUD操作實現控制器並啓用對數據網格的編輯。就像上面的例子。 – Sergey

+0

leman17希望將更改後的網格數據與其他數據一起保存在其表單中,並以單一表單POST提交。我想知道如何完成這項任務。 – Sven

2

你可以使用cellTemplate爲每一列和cellTemplate你必須使用隱藏輸入。例如假設此代碼:

factory.Add().DataField("Mobile") 
      .CellTemplate(@<text> 
       <%= text %> 
       <input type='hidden' name='Addresses[<%= rowIndex %>].Mobile' value='<%= value %>' /> 
       </text>); 

text,rowIndex,value是DevExtreme對象。 欲瞭解更多信息,請檢查cellInfo部分鏈接。 https://js.devexpress.com/Documentation/ApiReference/UI_Widgets/dxDataGrid/Configuration/columns/?search=columns

+0

請在鏈接周圍添加一些上下文,複製相關部分並將其粘貼在一起,以防網站內容發生更改或不可用。 – LW001

+0

這仍然是唯一有效的解決方案嗎? – Sven

+0

@Sven如果你的網格有一個頁面或服務器操作是false,你可以使用getDataSource()來代替。例如:var allGridData = $(「#yourGridId」)。dxDataGrid('instance')。了getDataSource(); –

相關問題