gpt4 book ai didi

javascript - 将行添加到 ASP.NET MVC 表时更新连接的数据库表

转载 作者:行者123 更新时间:2023-12-03 01:36:01 25 4
gpt4 key购买 nike

以下代码构成了我当前正在开发的 ASP.NET MVC 应用程序的一部分。该索引创建一个表,用户可以通过在弹出模式中输入标签、服务器和频率的值来添加行(通过点击“添加”按钮激活,模式 HTML 代码未显示)。该表的初始值当前是通过从链接的 SQL 数据库表(使用 Entity Framework 创建)迁移生成的。

我正在尝试修改此代码,以便通过“添加”按钮添加的任何行都将自动添加到链接的数据库表中(最好使用 Entity Framework )。任何帮助将不胜感激。

Controller

namespace ExampleWebAppilcationTest.Controllers
{
public class HomeController : Controller
{
ExampleDB _db = new ExampleDB();

public ActionResult Index()
{
var model = _db.TData.ToList();
return View(model);
}

protected override void Dispose(bool disposing)
{
if (_db != null)
{
_db.Dispose();
}
base.Dispose(disposing);
}
}
}

类(class)

namespace ExampleWebAppilcationTest
{
public class ExampleDB : DbContext
{
public DbSet<TableData> TData { get; set; }
}
}



namespace ExampleWebAppilcationTest
{
public class TableData
{
[Key]
public String Tag { get; set; }
public String Server { get; set; }
public double Frequency { get; set; }
}
}

索引

@model IEnumerable<ExampleWebAppilcationTest.TableData>

@{
ViewBag.Title = "Home Page";
}

@{
ViewBag.Title = "Index";
}

<h2>Table Data</h2>

<table class="table table-bordered" id="mainTable">
<thead>
<tr>
<th></th>
<th class="thTag" scope="col">
@Html.DisplayNameFor(model => model.Tag)
</th>
<th class="thServer" scope="col">
@Html.DisplayNameFor(model => model.Server)
</th>
<th class="thFreq" scope="col">
@Html.DisplayNameFor(model => model.Frequency)
</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="5">

@foreach (var item in Model)
{
<tr>
<td><input type="checkbox"/></td>
<td>
@Html.DisplayFor(modelItem => item.Tag)
</td>
<td>
@Html.DisplayFor(modelItem => item.Server)
</td>
<td>
@Html.DisplayFor(modelItem => item.Frequency)
</td>
</tr>
</tbody>
</table>

<button type="button" id="addBtn" class="btn btn-success">Add</button>

<!-- The Modals -->

<script>

var table = document.getElementById('mainTable');
// Get the modal
var addmodal = document.getElementById('addModal');

// When the user clicks the button, open the modal
btn.onclick = function () {
addmodal.style.display = "block";
}

var sbtn = document.getElementById("subBtn");
sbtn.onclick = function () {
var table = document.getElementById("mainTable");
var tag = document.getElementById("tag").value;
var server = document.getElementById("server").value;
var frequency = document.getElementById("frequency").value;
var objInputCheckBox = document.createElement("input");
objInputCheckBox.type = "checkbox";
var row = table.insertRow(-1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
var cell4 = row.insertCell(3);
cell1.appendChild(objInputCheckBox);
cell2.innerHTML = tag;
cell3.innerHTML = server;
cell4.innerHTML = frequency;
addmodal.style.display = "none";

}

最佳答案

尽管您的项目应该有一个分层架构,其中包含单独的业务层和数据访问层,并且 Controller 应该只是传入请求的网关 https://learn.microsoft.com/en-us/dotnet/standard/microservices-architecture/microservice-ddd-cqrs-patterns/infrastructure-persistence-layer-design )

这是您可以对当前调整执行的操作:

Controller :

namespace ExampleWebAppilcationTest.Controllers
{
public class HomeController : Controller
{
[HttpGet]
public ActionResult Index()
{
using (var dbContext = new ExampleDB())
{
var model = dbContext.TData.ToList();
return View(model);
}
}

[HttpPost]
public ActionResult Index(TableData data)
{
using (var dbContext = new ExampleDB())
{
dbContext.TData.Add(data);
dbContext.SaveChanges();
}

return RedirectToAction("Index");
}
}
}

数据访问

namespace ExampleWebAppilcationTest
{
public class ExampleDB : DbContext
{
public ExampleDB() : base(nameOrConnectionString: "Your Database Connection String") { }
public DbSet<TableData> TData { get; set; }
}
}



namespace ExampleWebAppilcationTest
{
public class TableData
{
[Key]
public String Tag { get; set; }
public String Server { get; set; }
public double Frequency { get; set; }
}
}

查看

sbtn.onclick = function () {
var table = document.getElementById("mainTable");
var tag = document.getElementById("tag").value;
var server = document.getElementById("server").value;
var frequency = document.getElementById("frequency").value;

//Here fetch all data in a class
var data = { Tag: tag, Server: server, Frequency: frequency };

//make ajax call to add data
$.ajax({
type: "POST",
url: '@Url.Action("Index", "Home")', //your action
data: data,
dataType: 'json',
success: function (result) {
//to close the popup
},
error: function (result) {
//to show error message
}
});
}

关于javascript - 将行添加到 ASP.NET MVC 表时更新连接的数据库表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51069371/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com