【问题标题】:How do I delete a line from the table in index page when I press the delete button without deleting it from the database当我按下删除按钮而不从数据库中删除它时,如何从索引页面的表中删除一行
【发布时间】:2017-07-04 08:48:15
【问题描述】:

当我按下删除按钮而不从数据库中删除它时,如何从索引页面的表中删除一行。 我将 asp.net mvc5 与 Visual Studio 2017 和 sql server 2016 一起使用。

// GET: Information/Delete/5
    public ActionResult Delete(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Information information = db.Informations.Find(id);
        if (information == null)
        {
            return HttpNotFound();
        }
        return View(information);
    }

    // POST: Information/Delete/5
    [HttpPost, ActionName("Delete")]
    [ValidateAntiForgeryToken]
    public ActionResult DeleteConfirmed(int id)
    {
        Information information = db.Informations.Find(id);
        db.Informations.Remove(information);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            db.Dispose();
        }
        base.Dispose(disposing);
    }
}

}

此代码从数据库中删除选定的行。我只想从索引页面中删除它。 请任何人给我任何提示。

【问题讨论】:

    标签: c# asp.net sql-server visual-studio model-view-controller


    【解决方案1】:

    如果你只从索引页面中删除它,该行将在页面加载时再次出现。

    但是,您可以使用 jquery 将其删除。

    在您的索引视图中写下:

    <script>
        $(".class").on("click", function(){   //if your element has a class
             $(this).remove();
        })
        $("#id").on("click", function(){     //if your element has an id
             $(this).remove();
        })
    </script>
    

    【讨论】:

    • 非常感谢!但我可以在哪里写这个?也许在这里? public ActionResult DeleteConfirmed(int id) { 信息信息 = db.Informations.Find(id); db.Informations.Remove(信息); db.SaveChanges(); return RedirectToAction("索引");对不起,我只是一个初学者!
    • 不,我写的代码是用 JavaScript 写的,而你的控制器是用 C# 写的。
    • 那么我如何用 C# 编写它?我应该在哪里写?
    • 我在索引视图代码的末尾编写了代码,但它仍然从我的数据库中删除了该行。我应该从控制器中删除这里的东西吗?: public ActionResult DeleteConfirmed(int id) { Information information = db.Informations.Find(id); db.Informations.Remove(信息); db.SaveChanges(); return RedirectToAction("索引"); }
    • 我的索引视图在每行之后有三个按钮,一个表格显示了我的数据库中的内容。按钮是编辑、详细信息和删除。当我单击删除按钮时,将加载另一个页面,显示所选行的详细信息和一个删除按钮。当我单击它时,该行将从表和数据库中消失。我希望它从桌子上消失。我的控制器中的代码删除了从数据库中选择的行,因此该行也从表中消失了,因为它显示了我的数据库中的内容..
    【解决方案2】:
     <script type="text/javascript">
            $(document).ready(function () {
          function deleteRow() {           
                    var par = $(this).parent().parent();
                    par.remove();
                };
                $("#Tableid").on("click", "#btnid", deleteRow);
        });
        </script>
    

    在索引页面中编写此代码

    "#Tableid"= 您的表的 ID,

    "#btnid" = 您的删除按钮的 id(对于表格的所有删除按钮都是唯一的)

    此代码仅删除页面级别的列,而不是从数据库中删除,

    如果你重新加载页面,你会取回所有数据

    如果有什么问题欢迎咨询

    【讨论】:

      【解决方案3】:

      您可以只使用 jquery remove() 方法来删​​除该行.. 只是你有一些东西可以针对该行,即 id 或一个类

      $('#ID').remove(); 或者 $('.CLASSNAME').remove();

      【讨论】:

        【解决方案4】:

        谢谢大家。当我在控制器中编写此代码时,我解决了这个问题: 返回视图(db.Informations.Where(f=>f.show== true).ToList()); “显示”是一个复选框。如果在创建页面中选择它,那么该行将出现在索引页面中,如果没有,它将只出现在数据库中。 所以我需要删除按钮只是为了删除所选行在索引页面中不可见。 我为我的英语不好道歉。 谢谢

        【讨论】:

          猜你喜欢
          • 2016-04-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-06-08
          • 1970-01-01
          • 2020-03-25
          相关资源
          最近更新 更多