如何在MVC中插入批量数据 [英] How to insert bulk data in MVC
本文介绍了如何在MVC中插入批量数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨朋友们,
我想将批量数据保存到数据库。
我改编了以下代码:
Hi friends,
I''m want to save bulk data to DB.
I refred following code:
[HttpPost]
public ActionResult Create(Order obj)
{
try
{
db.Order.Add(obj);
db.SaveChanges();
return RedirectToAction("Index");
}
catch (DataException)
{
}
}
在上面的例子中,我只能保存一行订单类型的数据,但我有表格,其中每一行代表客户添加的订单。
如何保存DB中的表格??
任何想法我该怎么办?
谢谢advance
In above case I can save only one row of data of type order, but I have table where each row represents the order added by customer.
how can I save the table in the DB??
Any idea how can I do it??
Thanks in advance
推荐答案
首先,您的Action只接受一个订单。尝试按照以下步骤操作:
First, your Action is accepting only one Order. Try making you Action as below:
[HttpPost]
public ActionResult Create(List<orders> orders)
{
try
{
}
catch (DataException)
{
}
}
</orders>
然后这样做:
Then do this:
[HttpPost]
public ActionResult Create(List<orders> orders)
{
try
{
foreach(var o in orders)
{
db.Order.Add(o);
//etc
}
}
catch (DataException)
{
}
}
</orders>
这篇关于如何在MVC中插入批量数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文