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
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