question

WongWeeYen-1110 avatar image
0 Votes"
WongWeeYen-1110 asked YihuiSun-MSFT answered

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

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





dotnet-aspnet-mvc
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

YihuiSun-MSFT avatar image
0 Votes"
YihuiSun-MSFT answered

Hi @WongWeeYen-1110,
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


view.txt (1.8 KiB)
6.gif (386.2 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.