2012-04-23 35 views
2

我有一張表,其中有幾行數據需要返回給控制器。在我看來,我最初通過選擇一個時間段並點擊一個按鈕來加載表格。該表加載所有相關記錄,但我的一個表格單元格包含一個下拉列表。所以我應該可以在下拉菜單中選擇「更新」,我的控制器保存更改。MVC:將多行數據返回給控制器

所以一切正常,直到我嘗試並保存。發送給控制器的模型是完全無效的。我綁定到表格單元格的列表屬性返回給控制器null。

@ModelType SuperViewModel 
//We need this a view model in order to store a List of the models in the table 

@Using (Html.BeginForm()) 
@For Each i in Model.CompleteList 
    Dim currentItem = i //MVC auto-generated extra declarations. Seems redundant to me but it works. 
@<table> 
<tr> 
<td>@Html.DisplayFor(function(Model)currentItem.Name)</td> 
<td>@Html.DisplayFor(function(Model)currentItem.SampleTime)</td> 
<td>@Html.DropDownListFor(function(Model)currentItem.WorkTime, ViewBag.WorkTimeList)</td> 
</tr> 
Next 
</table> 
<input name="submit" type="submit" value="Update"/> 
End Using 

//Controller 
<HttpPost()> 
function Save(vmodel as SuperViewModel, submit as String) as ActionResult //NOTE: submit parameter is used because we have two submit buttons but its not relevant here 
     if submit = "Update" 
      db.Entry(vmodel.CompleteList).State = EntityState.Modified//Here the exception is throw because our list is null at this point even tho its tied to the model in the view. 
      db.SaveChanges() 
     end if 
End Function 

注意:這是用VB.NET編寫的,但歡迎使用C#幫助。我在MVC中熟悉這兩種語言。

+0

SuperViewModel是如何定義的? – 2012-04-23 23:03:18

+0

它有幾個屬性,但在這裏適用的是「CompleteList」,它是一個List(Of AnotherModel)。實體框架基於我們的數據庫表生成的那個其他模型。 – ExceptionLimeCat 2012-04-23 23:08:26

+0

在「End Using」關閉「For Each」之前是否存在'Next'?另外,你可以添加一些由視圖呈現的標記嗎? – 2012-04-23 23:33:26

回答

2

您需要使用for iterator並讓視圖使用HTML元素的索引元素。我不知道VB.NET語法,c#示例如下。這允許模型聯編程序正確地確定視圖模型中的元素,以便它可以在回發時重新填充視圖模型。

<table> 
@For(var i = 0; i < Model.CompleteList.Count; i++) 
{ 
<tr> 
<td>@Html.DisplayFor(Model.CompleteList[i].Name)</td> 
<td>@Html.DisplayFor(Model.CompleteList[i]..SampleTime)</td> 
<td>@Html.DropDownListFor(Model.CompleteList[i]..WorkTime, ViewBag.WorkTimeList)</td> 
</tr> 
} 
</table> 

您的文章方法應該工作得很好。

+0

這是正確的。我們沒有將我們的行綁定到我們列表中的特定實例。謝謝。 – ExceptionLimeCat 2012-04-26 19:05:31

0

由於參數名稱中的某些衝突(所使用的參數名稱必須在其他地方使用),它可能會返回null。試試這個

<input name="subButton" type="submit" value="Update"/> 

,改變

function Save(vm as SuperViewModel, subButton as String) as ActionResult 
+0

那沒用。 SuperViewModel在擊中控制器時仍爲空。 – ExceptionLimeCat 2012-04-23 23:30:10

+0

參考這[博客文章](http://codeblog.shawson.co.uk/mvc-strongly-typed-view-returns-a-null-model-on-post-back/) – 2012-04-23 23:31:03

+0

該博客沒有真的有幫助。我添加了ModelState驗證。它通過,如果但視圖模型仍然爲空。 – ExceptionLimeCat 2012-04-23 23:46:24

0

我需要看到由DropDownListFor生成的標記名稱,但我懷疑這是因爲currentItem你對CompleteList一種類型的結合,但您的控制器方法期待完整的SuperViewModel類型。您所顯示的代碼中沒有任何地方顯示我看到您綁定到SuperViewModel類型。

試着改變你的控制器方法:

<HttpPost()> 
function Save(currentItem as CompleteList, submit as String) as ActionResult //NOTE: submit parameter is used because we have two submit buttons but its not relevant here 
     if submit = "Update" 
      db.Entry(vmodel).State = EntityState.Modified//Here the exception is throw because our list is null at this point even tho its tied to the model in the view. 
      db.SaveChanges() 
     end if 
End Function 

WorkTime屬性應該從DropDownList中選定的值來填充。

編輯:更改參數名稱以匹配綁定名稱。

+0

這是下拉列表的標記。你是說'For Each'下面的第二個聲明導致MVC無法識別對模型的綁定? – ExceptionLimeCat 2012-04-24 00:05:58

+0

有趣。我不熟悉的模型結合的VB版本,但你得到什麼(或空)的原因是因爲模型綁定無法將值映射到控制器參數。它看起來像模型期望映射到名爲'currentItem As CompleteList'的東西。 – mgnoonan 2012-04-24 00:06:36

相關問題