How to save all record of DataTables into two table of samedatabase when pressing button using Jquery MVC

Wong Wee Yen 1 Reputation point
2021-06-04T07:18:08.907+00:00

Dear all,

I am lack of experience dataTables and MVC programming language but will challenge on myself. I also try to read a forum and many surf net but i am hang for few days that how to save all record from DataTables to multiple table of same database when press "Import" button using Jquery MVC.

Please help to give idea / reference to me. Thank you for advance.

Below sample screen.

102356-view-datatables-on-screen.jpg

Developer technologies ASP.NET Other
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Yihui Sun-MSFT 806 Reputation points
    2021-06-07T06:40:26.737+00:00

    Hi @Wong Wee Yen ,
    You can use ajax to send the data in the form to the action.
    I wrote an example, you can refer to it.

    Model

        public class Other1  
        {  
            [Key]  
            [DatabaseGenerated(DatabaseGeneratedOption.Identity)]  
            public int Id { get; set; }  
            public string Other1Name { get; set; }  
        }  
        public class Other2  
        {  
            [Key]  
            [DatabaseGenerated(DatabaseGeneratedOption.Identity)]  
            public int Id { get; set; }  
            public string Other2Name { get; set; }  
        }  
    

    Controller

        public class JqueryDatablesController : Controller  
        {  
            public DailyMVCDemoContext db = new DailyMVCDemoContext();  
            // GET: JqueryDatables  
            public ActionResult Index()  
            {  
                return View();  
            }  
            // Assuming the data of DataTable  
            public ActionResult getdata()  
            {  
                var data = new List<TestViewModel>();  
                for (int i = 1; i < 10; i++)  
                {  
                    data.Add(new TestViewModel { Other1= "Other1Name"+i.ToString(), Other2= "Other2Name" + i.ToString() });  
                }  
                return Json(new { testdata = data }, JsonRequestBehavior.AllowGet);  
            }  
            //insert data  
            [HttpPost]  
            public ActionResult insert(List<TestViewModel> data)  
            {  
                var other1 = new List<Other1>();  
                var other2 = new List<Other2>();  
                data.ForEach(d =>  
                {  
                    other1.Add(new Other1 { Other1Name = d.Other1 });  
                    other2.Add(new Other2 { Other2Name = d.Other2 });  
                });  
                var message = "";  
                try  
                {  
                    db.Other1.AddRange(other1);  
                    db.Other2.AddRange(other2);  
                    db.SaveChanges();  
                }catch(Exception e)  
                {  
                    message = e.Message;  
                }  
                return Json(message, JsonRequestBehavior.AllowGet);  
            }  
        }  
    

    View(In the text file)
    102913-view.txt
    Result
    102898-6.gif


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
    Best Regards,
    YihuiSun

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.