Asp.net mvc 3 我的模型应该如何将动态添加的文本框中的数据存储在数据库中?
我的模型:Asp.net mvc 3 我的模型应该如何将动态添加的文本框中的数据存储在数据库中?,asp.net-mvc-3,entity-framework-4,Asp.net Mvc 3,Entity Framework 4,我的模型: 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&
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>
调试时,我可以看到order对象填充了表单中的数据,但只填充了第一个quantity、weight和height文本框的值,我知道这是预期的行为,我知道我收到了所有值,因为我像下面的示例一样进行了测试,并且我的列表正确填充:
[HttpPost]
public ActionResult SaveOrder(List<int> Quantity, List<int> Width, List<int> Height)
{
return View();
}
[HttpPost]
public ActionResult保存顺序(列表数量、列表宽度、列表高度)
{
返回视图();
}
所以这里又是一个问题:我应该如何创建一个模型来保存数据数组并保存在我的数据库中
我希望我已经清楚地回答了这个问题,并感谢您的回答。您的视图模型必须有一个要添加到的列表。无法将模型添加到singletone的固定实例中(视图中有
@model
)。因此,您的模型必须是可扩展的
这是你的样品
看看他们做了什么,对你的控件和视图做同样的事情。我完全基于本文构建了我的应用程序-它足够简单,即使是新手(具备基本知识)也能理解,并在您的领域中实现类似的功能。
他们也有完整的演示源代码,你可以下载和玩
用简单的话来描述这个过程实在太长了
textbox
,在您的情况下,它肯定会更复杂)和$().append()
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; }
}
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);
}
[HttpPost]
public ActionResult保存顺序(订单顺序、列表数量、列表宽度、列表高度)
{
列表度量值=新列表();
//此时,我的订单模型充满了所有属性
//它来自表单,我会将它保存到数据库中,以便
//此模型可以从标识列中填充其Id
unitOfWork.ServicoRepository.Insert(订单);
unitOfWork.Save();
//现在我有了OrderId,我可以添加需要引用的度量值
//该OrderId可以在我的视图上进一步访问它
for(int i=0;i
在您的视图模型中尝试公共列表订单{get;set;}。我不明白,您的意思是在我的订单模型中添加订单列表吗?不完全正确。使用视图模型而不是域模型来绑定输入。在视图模型中具有订单对象列表。然后在控制器中,您可以迭代列表,并在每次迭代中创建/填充/编辑一个单独的order对象。感谢您的回答,但我已经传递了这些问题,并找到了这些问题,但这不是我要寻找的。我理解这些示例中的内容,但是,假设我有一个参考礼物模型的人物模型,当添加一个人时,我想添加尽可能多的礼物,我应该怎么做?这就是我想问的,我的情况下,我需要有许多数量,重量和高度相同的订单号,这是可能的吗?肯定是可能的。我理解你的问题。我会这样做:我认为有天赋的人的天赋数量是可变的。一旦你回发,你就可以通过它的Id知道它是什么人,这个Id最初是由同名的get action设置的(将它保存在@HiddenFor
位置)。请让我知道,如果你有任何问题,实现这一点(根据文章你的人是固定的,你的变量是礼物)。谢谢
[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);
}