Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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) 
    { 
    }   
} 


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
Posted
Comments
Richard C Bishop 8-Apr-13 10:37am    
You will need to iterate through each row of data and add them. Use a foreach loop.
dhage.prashant01 9-Apr-13 1:17am    
One way i know is through Jquery
loop through each tr and create CSV and send to action at controller side??

Any other way to do the same??
Richard C Bishop 9-Apr-13 9:42am    
If you know that way, I would stick with it.
dhage.prashant01 10-Apr-13 1:10am    
I want to do in sum other way, any other technique?
Richard C Bishop 10-Apr-13 9:50am    
I told you what to use in my first comment. Use a foreach loop. Do a search on how to use one, it is very simple.

1 solution

First, your Action is accepting only one Order. Try making you Action as below:

C#
[HttpPost] 
public ActionResult Create(List<orders> orders) 
{ 
    try 
    { 
        
    } 
    catch (DataException) 
    { 
    }   
} 
</orders>


Then do this:

C#
[HttpPost] 
public ActionResult Create(List<orders> orders) 
{ 
    try 
    { 
        foreach(var o in orders)
        {
           db.Order.Add(o);
           //etc
        }
    
    } 
    catch (DataException) 
    { 
    }   
} 
</orders>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900