2012-08-10 50 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

相关问题