C# 编写与JQuery配合使用以更新数据库的控制器操作时出现问题
我有一个html表格,其中包含了各种类别的内容。每一行由一个类别id和名称组成,这些id和名称是从模型循环进来的。每行还有两个按钮。一个用于将行的状态设置为enabled,另一个用于将其设置为disabled:C# 编写与JQuery配合使用以更新数据库的控制器操作时出现问题,c#,jquery,sql-server,asp.net-mvc,entity-framework,C#,Jquery,Sql Server,Asp.net Mvc,Entity Framework,我有一个html表格,其中包含了各种类别的内容。每一行由一个类别id和名称组成,这些id和名称是从模型循环进来的。每行还有两个按钮。一个用于将行的状态设置为enabled,另一个用于将其设置为disabled: <table id="categoryList" class="table"> <thead> <tr> <th>Category ID</th> <th>Catego
<table id="categoryList" class="table">
<thead>
<tr>
<th>Category ID</th>
<th>Category Name</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.Categories)
{
<tr>
<td>@item.id</td>
<td>@item.name</td>
<td>
<button class="btn btn-success categoryEnabled">Enabled</button>
<button class="btn btn-danger categoryDisabled" style="display: none;">Disabled</button>
</td>
</tr>
}
</tbody>
</table>
您可以看到,我正在尝试将这些连接到CategoryController中的控制器操作。我只是不太确定应该在这些操作中添加什么,以便使用实体框架连接到Category表的state列
public ActionResult DisableRow()
{
}
public ActionResult EnableRow()
{
}
我还需要一个循环来标识我试图更新的行:
int i = 0;
foreach (var row in Model.Rows)
{
string id = "Row" + i.ToString() + "Enable";
i +=1;
}
如果您看到我拥有的category对象,也会很有帮助:
namespace Rework.Models
{
using System;
using System.Collections.Generic;
public enum StatesTypes
{
Disabled = 0,
Enabled = 1
}
public partial class Category
{
public int id { get; set; }
public string name { get; set; }
public StatesTypes state { get; set; }
}
}
数据库上下文:
namespace Rework.Models
{
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
public partial class CategoryDBEntities : DbContext
{
public CategoryDBEntities()
: base("name=CategoryDBEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public DbSet<Category> Categories { get; set; }
}
namespace.Models
{
使用制度;
使用System.Data.Entity;
使用System.Data.Entity.Infrastructure;
公共部分类categoryDBenties:DbContext
{
公共类别()
:base(“name=categorydbenties”)
{
}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
抛出新代码FirstException();
}
公共数据库集类别{get;set;}
}
}
只是不确定是否有更好的方法,或者这是否适合。谢谢你的帮助 好的,首先,jQuery实际上没有获得ID。请尝试将代码更新为类似这样的内容
<table id="categoryList" class="table">
<thead>
<tr>
<th>Category ID</th>
<th>Category Name</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.Categories)
{
<tr>
<td>@item.id</td>
<td>@item.name</td>
<td>
<button class="btn btn-success categoryEnabled" data-id="@item.id">Enabled</button>
<button class="btn btn-danger categoryDisabled" data-id="@item.id" style="display: none;">Disabled</button>
</td>
</tr>
}
</tbody>
</table>
接下来,您需要更新您的操作。我不知道你为什么要返回ActionResult,因为看起来你不需要任何具体的响应。我不知道您使用什么对象连接到数据库上下文,因此需要替换此对象
public void DisableRow(int id)
{
//
// Create a instance of your EF database context
//
var context = new CategoryDBEntities();
var record = context.Categories.find(id);
if (record != null)
{
record.state = StateTypes.Disabled;
context.SaveChanges();
}
}
public void EnableRow(int id)
{
//
// Create a instance of your EF database context
//
var context = new CategoryDBEntities();
var record = context.Categories.find(id);
if (record != null)
{
record.state = StateTypes.Enabled;
context.SaveChanges();
}
}
非常感谢,我添加了dbcontext只是为了提供帮助。当我尝试更新时,会收到错误警报。逐步了解可能发生的事情。statestypes.enabled和disabled值在输入位字段时应该没有问题,对吗?我以前没有在表中使用位数据类型。您刚刚将此属性添加到模型中吗?您可能需要更新数据库,因为您的模型和数据库将不同步。首先检查代码迁移并使用EF代码更新数据库。或者,如果这是一个开发环境,您可以直接删除并创建数据库againDo您的意思是将它添加到我的DbContext?我的意思是您刚刚将status属性添加到Category模型中了吗?如果有,则数据库和模型将不同步,因为数据库中不存在此新列。那是你的错误吗?
$(".categoryEnabled").on("click", function () {
$(this).hide();
$(this).next().show();
DisableRow($(this).data('id'));
});
$(".categoryDisabled").on("click", function () {
$(this).hide();
$(this).prev().show();
EnableRow($(this).data('id'));
});
function EnableRow(id) {
$.post("@Url.Action('DisableRow', 'Category')", { "Id": id }, function () {
alert('Row Disabled!');
}).fail(function() {
alert('Error disabling row!');
});
}
function DisableRow(id) {
$.post("@Url.Action('EnableRow', 'Category')", { "Id": id }, function() {
alert('Row Enabled!');
}).fail(function () {
alert('Error enabling row!');
});
}
public void DisableRow(int id)
{
//
// Create a instance of your EF database context
//
var context = new CategoryDBEntities();
var record = context.Categories.find(id);
if (record != null)
{
record.state = StateTypes.Disabled;
context.SaveChanges();
}
}
public void EnableRow(int id)
{
//
// Create a instance of your EF database context
//
var context = new CategoryDBEntities();
var record = context.Categories.find(id);
if (record != null)
{
record.state = StateTypes.Enabled;
context.SaveChanges();
}
}