2012-08-10 30 views
3

我的模型:我的模型應該如何將數據從動態添加的文本框中存儲到數據庫中?

public class Order 
{ 
    public int OrderId { get; set; } 
    public int Quantity { get; set; } 
    public double Width { get; set; } 
    public double Height { get; set; } 
} 

在我的那個模型強類型的視圖我有TextBoxFor對這樣性質的形式:

<table> 
    <tr> 
     <td><a href="#" onclick="AddTextBox()">+</a></td> 
     <td>@Html.TextBoxFor(m => m.Quantity)</td> 
     <td>@Html.TextBoxFor(m => m.Width)</td> 
     <td>@Html.TextBoxFor(m => m.Height)</td> 
    </tr> 
</table> 

AddTextBox()的聯繫是一個jQuery函數動態添加另一個包含三個以上文本框的表格。

我的控制器:

[HttpPost] 
public ActionResult SaveOrder(Order order) 
{ 
    context.Order.Add(order); 
    context.SaveChanges(); 
    return View(); 
} 

調試時,我可以看到訂單對象中滿是形式,但僅與第一數量,重量和高度的文本框的值的數據,我知道這是預期行爲,我知道我收到的所有值,因爲我測試如下面的例子,我的解釋得到了正確填寫:

[HttpPost] 
public ActionResult SaveOrder(List<int> Quantity, List<int> Width, List<int> Height) 
{ 
    return View(); 
} 

因此,這裏的問題又來:我應該如何創建一個模型來保存數據的數組並保存在我的數據庫?

我希望我清楚的問題,並感謝您的答案。

+0

嘗試公開列表 Orders {get;設置;}在你的視圖模型。 – 2012-08-10 15:50:03

+0

我不明白,是否在我的訂單模型中添加訂單清單? – 2012-08-10 16:04:12

+0

不完全。使用視圖模型而不是您的域模型來綁定輸入。在視圖模型中有一個訂單對象列表。然後在控制器中,您可以遍歷列表並在每次迭代中創建/填充/編輯一個單獨的訂單對象。 – 2012-08-10 16:27:22

回答

0

您的視圖模型必須包含要添加到的列表。您無法將模型添加到singletone的固定實例(其中@model位於您的視圖中)。所以你的模型必須是可擴展的。

這裏是你的樣品,Editing a variable length list, ASP.NET MVC

看看他們做了什麼,你的控制和視圖做同樣的。我完全基於這篇文章構建了我的應用程序 - 它的簡單程度足以讓新手甚至可以理解(具有基本知識)並在您的領域內實現類似的功能。 他們也有充分的演示源代碼,你可以下載和播放

它只是太長描述的過程中,在簡單的話,你

  1. 在你的控制器創建專門的行動,並使用AJAX調用它生成任何你想添加到你的視圖(textbox在你的情況,但它可以肯定是更復雜的東西)和

  2. $(<your selector>).append(<your result from ajax call>)

  3. 上發佈回控制器,你得到的名單與實際項目數您添加/在你的視圖中刪除

  4. 一個接一個將它們添加到您的DTO,當你完成提交事務。

希望這會有所幫助。

+0

感謝您的回答,但我已經通過這個問題已經發現這個[鏈接](http://stackoverflow.com/questions/223149/how-do-you-handle-the-output-of-a-dynamically -generated-form-in-asp-net-mvc),但它不是我正在尋找的。 – 2012-08-10 16:30:10

+0

我明白這個例子裏有什麼,但是讓我們假設我有一個Person模型引用了Gift模型和添加一個人時,我想添加儘可能多的禮物,我應該怎麼做?這就是我想問的問題,我的情況是我需要使用相同的OrderId有很多數量,重量和高度,這有可能嗎? – 2012-08-10 20:46:19

+0

絕對有可能。我明白你的問題。我會這樣做:1對於有禮物的人來說,他們的數量是可變的。一旦你回發,你知道它是由什麼人的Id,這是最初通過獲取具有相同名稱的行動設置(保留在@ @ HiddenFor'地方)。 請讓我知道如果你有任何問題實施(根據該文章你的人是固定的,你的變量是禮物)。 謝謝。 – 2012-08-11 00:00:59

0

終於!

我不知道我是否對我的問題非常清楚,但這裏是我找到的解決方案,我不知道這是否很簡單,因爲我對編程不是很有經驗,所以非常感謝誰試圖幫助我。

只有一個型號的Istead我不得不對創建另一個保存在我的數據庫中的所有,每一個訂單有辦法,所以我改變了我的模型看起來像這樣:

public class Order 
{ 
    public int OrderId { get; set; } 
    public virtual List<Measures> Measures { get; set;} 
    // I have many other properties here but i erased for simplicity... 
} 

public class Measures 
{ 
    public int MeasuresId { get; set; } 
    public int Quantity { get; set; } 
    public double Width { get; set; } 
    public double Height { get; set; } 
    public Order Order { get; set; } 
} 

所以我的看法加入一些<input> dinamically畢竟他們的name屬性是數量,寬度或高度和我收到了我的控制器上的反應是這樣的:

Quantity: 10 
Width: 20 
Height: 16 
Quantity: 3 
Width: 14 
Height: 10 
Quantity: 500 
Width: 2 
Height: 5 

最後,我的控制器:

[HttpPost] 
    public ActionResult SaveOrder(Order Order, List<int> Quantity, List<double> Width, List<double> Height) 
    { 
     List<Measures> Measures = new List<Measures>(); 

     //At this point my Order model is filled with all the properties 
     //that came from the form and I'll save it to the Database so that 
     //this model can have its Id filled from the identity column 
     unitOfWork.ServicosRepository.Insert(Order); 
     unitOfWork.Save(); 

     //Now that I have the OrderId I can add how much Measures I want referencing 
     //that OrderId to access that further on my View 
     for (int i = 0; i < Quantity.Count; i++) 
     { 
      Measures measures = new Measures { Quantity = Quantity[i], Width = Width[i], Height = Height[i], Order = Order }; 
      unitOfWork.MedidasRepository.Inserir(medidas); 
      unitOfWork.Salva(); 
      Medidas.Add(medidas); 
     } 

     return PartialView("_Emitir", Pedido); 
    } 
+0

很高興你明白了,謝謝! – 2012-08-13 15:14:51

相關問題