我有一个包含各类内容的html表。每行包含从模型中循环的类别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">Enabled</button>
<button class="btn btn-danger categoryDisabled" style="display: none;">Disabled</button>
</td>
</tr>
}
</tbody>
</table>
当我说设置为启用或禁用时,我的意思是在名为state的SQL表列中更改该行的位值。所以,我基本上只需要按钮来切换单击它的行的位值。
以下是我对解决方案的看法:
这是我用来尝试在单击每个按钮时更改所选行的位值的Jquery:
$(".categoryEnabled").on("click", function () {
$(this).hide();
$(this).next().show();
DisableRow($(this).attr('id'));
});
$(".categoryDisabled").on("click", function () {
$(this).hide();
$(this).prev().show();
EnableRow($(this).attr('id'));
});
function EnableRow(id) {
$.post("@Url.Action("DisableRow", "Category")", { "Id": id }, function (response) {
if (response.success) {
alert('Row Disabled!');
} else {
alert('Error disabling row!');
}
});
}
function DisableRow(id) {
$.post("@Url.Action("EnableRow", "Category")", { "Id": id }, function (response) {
if (response.success) {
alert('Row Enabled!');
} else {
alert('Error enabling row!');
}
});
您可以看到我尝试将这些连接到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;
}
您可以看到我所拥有的类别对象也很有帮助:
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; }
}
}
Db背景:
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; }
}
}
不确定是否有更好的方法或适合的地方。感谢您的帮助!
答案 0 :(得分:0)
好的,首先你的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>
现在,ID是我们可以使用此关键字访问它的按钮的一部分
$(".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!');
});
}
接下来,您需要更新您的操作。我不知道你为什么要返回一个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();
}
}