编写使用JQuery更新数据库的控制器操作时出现问题

本文关键字:操作 控制器 问题 数据库 JQuery 更新 | 更新日期: 2023-09-27 17:59:37

我有一个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>

当我说设置为enabled或disabled时,我的意思是更改SQL表中名为state的列中该行的位值。所以,我基本上只需要按钮来切换点击行的位值。

以下是我的解决方案:

以下是我用来尝试在单击每个按钮时更改所选行的位值的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; }
}

}

只是不确定是否有更好的方法或在哪里适合。谢谢你的帮助!

编写使用JQuery更新数据库的控制器操作时出现问题

好吧,首先你的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是按钮的一部分,我们可以使用this关键字访问它

$(".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();
    }
}