I'm new to ASP.NET MVC. I just know how to create more rows in a datatable.
I want to connect it with a database but can't figure out a way to do it.
I already create the controller, model and data class, and already finished Delete, Edit, Index methods.
I just need create method to finish the whole project.
I appreciate every answer to my question.
var table = null;
var arrData = [];
var arrDataPG = [];
arrData.push({
STT: 1,
id: 1,
product_type: "",
condition1: "",
});
$(document).ready(function () {
InitTable();
});
function InitTable() {
if (table !== null && table !== undefined) {
table.destroy();
}
table = $('#tableh').DataTable({
data: arrData,
"columns": [
{ "width": "25px" },
{ "width": "300px" },
{ "width": "300px" },
{ "width": "25px" },
],
columnDefs: [
{
title: "STT",
targets: 0,
data: null,
render: function (data, type, row, meta) {
return (meta.row + meta.settings._iDisplayStart + 1);
},
},
{
title: "Loại sản phẩm*",
targets: 1,
data: null,
render: function (data, type, row, meta) {
return '<textarea style="width: 300px;" id="product_type' + data.id + '" type="text" onchange="ChangeProductType(\'' + data.id + '\',this)" name="' + data.id + '" value="' + data.product_type + '">' + data.product_type + '</textarea>';
}
},
{
title: "Điều kiện*",
targets: 2,
data: null,
render: function (data, type, row, meta) {
return '<textarea style="width: 300px;" id="condition1' + data.id + '" type="text" onchange="ChangeCondition1(\'' + data.id + '\',this)" name="' + data.id + '" value="' + data.condition1 + '">' + data.condition1 + '</textarea>';
}
},
{
title: "",
targets: 3,
data: null,
className: "dt-center",
width: "70",
render: function (data, type, row, meta) {
// return ' <i style="cursor: pointer;font-size: 25px;padding-bottom: 30px;" class="fa fa-trash removePG" aria-hidden="true" onclick=removePG(\'' + data.id + '\')></i>';
return '<div class="btn btn-danger removePG" style="cursor: pointer;font-size:25px;" ><i class="fa-solid fa-trash"></i></div>';
}
},
],
});
table.columns.adjust().draw();
}
$('#addRow').on('click', function () {
console.log(arrData.length);
if (arrData != '') {
var ida = arrData[0].id;
} else {
var ida = 1;
}
for (var i = 0; i < arrData.length; i++) {
if (arrData[i].id > ida) {
ida = arrData[i].id;
}
};
arrData.push({
STT: ida + 1,
id: ida + 1,
product_type: "",
condition1: "",
});
if (table != null) {
table.clear();
table.rows.add(arrData).draw();
}
});
$('#tableh').on('click', '.removePG', function () {
var tableq = $('#tableh').DataTable();
tableq
.row($(this).parents('tr'))
.remove()
.draw();
});
function removePG(idc) {
let id = parseInt(idc);
if (arrData !== undefined) {
var find = arrData.find(function (item) {
return item.id === id;
});
if (find !== undefined) {
arrData = arrData.filter(function (item, index) {
return item.id !== id;
});
};
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet"type="text/css"href="https://cdn.datatables.net/1.12.1/css/jquery.dataTables.min.css">
<table id="tableh" class="cell-border hover" style="width:100%"></table>
<button style="width: 86px;" id="addRow" class="btn btn-success add">Addrow<i class="fa fa-plus" aria-hidden="true"></i></button>
<script src="https://code.jquery.com/jquery-3.5.1.js"></script>
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.12.1/js/jquery.dataTables.js"></script>
<script src="https://kit.fontawesome.com/60bf89e922.js" crossorigin="anonymous"></script>
Here is my Controller and Model
using Microsoft.AspNetCore.Mvc;
using WebApplication1.Data;
using WebApplication1.Models;
namespace WebApplication1.Controllers
{
public class FirstrowController : Controller
{
private readonly ApplicationDbContext _db;
public FirstrowController(ApplicationDbContext db)
{
_db = db;
}
public IActionResult Index()
{
IEnumerable<Firstrow> objFirstrowList = _db.Firstrow;
return View(objFirstrowList);
}
//Get
public IActionResult Create()
{
return View();
}
//POST
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Create(Firstrow obj)
{
if (obj.product_type == obj.condition1.ToString())
{
ModelState.AddModelError("name", "Nhập thiếu kìa fen");
}
if (ModelState.IsValid)
{
_db.Firstrow.Add(obj);
_db.SaveChanges();
return RedirectToAction("Index");
}
return View(obj);
}
//Get
public IActionResult Edit(int? id)
{
if (id == null || id == 0)
{
return NotFound();
}
var FirstrowFromDb = _db.Firstrow.Find(id);
if (FirstrowFromDb == null)
{
return NotFound();
}
return View(FirstrowFromDb);
}
//POST
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Edit(Firstrow obj)
{
if (obj.product_type == obj.condition1.ToString())
{
ModelState.AddModelError("name", "Nhập thiếu kìa fen");
}
if (ModelState.IsValid)
{
_db.Firstrow.Update(obj);
_db.SaveChanges();
return RedirectToAction("Index");
}
return View(obj);
}
//Get
public IActionResult Delete(int? id)
{
if (id == null || id == 0)
{
return NotFound();
}
var FirstrowFromDb = _db.Firstrow.Find(id);
if (FirstrowFromDb == null)
{
return NotFound();
}
return View(FirstrowFromDb);
}
//POST
[HttpPost,ActionName("Delete")]
[ValidateAntiForgeryToken]
public IActionResult DeletePOST(int? id)
{
var obj = _db.Firstrow.Find(id);
if (obj == null)
{
return NotFound();
}
_db.Firstrow.Remove(obj);
_db.SaveChanges();
return RedirectToAction("Index");
}
}
}
using System.ComponentModel.DataAnnotations;
namespace WebApplication1.Models
{
public class Firstrow
{
[Key]
public int id { get; set; }
[Required]
public string product_type { get; set; }
public string condition1 { get; set; }
}
}