【问题标题】:Button submit not responding with modal in ASP.NET MVC按钮提交在 ASP.NET MVC 中没有响应模式
【发布时间】:2021-12-15 02:38:20
【问题描述】:

我不知道为什么我的“提交”按钮没有响应。当我点击每个id中的编辑按钮时,它可以显示像这张图片这样的模态,因此数据被发送到模态。但是当我尝试编辑此数据并单击按钮时,它没有响应。

这是我的 Index.cshtml

<div class="card-body">
                <table id="datatablesSimple">
                    <thead>
                        <tr style="background-color:gray">
                            <th>No</th>
                            <th>Name</th>
                            <th>Action</th>
                        </tr>
                    </thead>
                    <tfoot>
                        <tr>
                            <th>No</th>
                            <th>Name</th>
                            <th>Action</th>
                        </tr>
                    </tfoot>
                    <tbody>
                        @{ var stt = 1;}
                        @foreach (var item in ViewBag.List)
                        {
                            var itemName = "#exampleModalss" + item.id_category;
                            var itemName1 = "exampleModalss" + item.id_category;
                            <tr>
                                <td>@stt</td>
                                <td>@item.name</td>
                                <td>
                                    <button type="button" class="btn btn-danger" data-bs-toggle="modal" 
                                            data-bs-target="@itemName">Edit</button>
                                </td>
                            </tr>
                            stt++;

                            <form action="/Category/edit" method="post">
                                <div class="modal fade" id="@itemName1" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
                                    <div class="modal-dialog modal-xl">
                                        <div class="modal-content">
                                            <div class="modal-header">
                                                <h5 class="modal-title" id="exampleModalLabel">Edit Category Book</h5>
                                                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                            </div>
                                            <div id="resultss" class="modal-body">
                                                <div class="row">
                                                    <div class="col">
                                                        <div class="">
                                                            <label for="category-film" class="col-form-label" style="font-weight:bold;width : 140px">Name Category: </label>
                                                            <input class="form-control" type="text" id="name" name="name" value=@item.name>
                                                            <input class="form-control" type="hidden" id="id_category" name="id_category" value=@item.id_category>
                                                        </div>
                                                    </div>

                                                </div>
                                            </div>
                                            <div class="modal-footer">
                                                <button style="width:100px" type="submit" class="btn btn-primary">Edit</button>
                                            </div>
                                        </div>

                                    </div>
                                </div>
                            </form>
                        }
                    </tbody>
                </table>
            </div>

我的控制器

[HttpPost]
        public ActionResult edit(FormCollection form)
        {
            Category cat = new Category();
            cat.id_category = Convert.ToInt32(form["id_category"]);
            category.edit(cat);
            return PartialView("Index", new { msg = "1" });
        }

我的 DAO

public void edit(Category category)
        {
            var result = myDb.categories.SingleOrDefault(c => c.id_category == category.id_category);
            result.name = category.name;
            myDb.SaveChanges();
        }

这是我单击带有类别书的每个 ID 的“编辑”按钮时的结果。 我被卡住了几天,任何人都可以帮助我。我觉得

已编辑详细信息

【问题讨论】:

  • 您有两个编辑按钮 - 一个在表单内,另一个在表单外。哪一个不适合你?
  • @Rahatur 提交按钮,我的朋友。在模态页脚中。
  • 首先,没有您所说的“数据发送到模态”。模态只是您的剃须刀页面生成的一段 HTML。它在单击按钮之前的页面上。它只是在按钮单击时显示。其次,要找出模式中包含的表单为什么不起作用,您需要进行调试。如果你在你的编辑后方法中设置一个断点,会发生什么?绑定是否有效(您在表单参数中得到什么)?验证有效吗?如果您什么也没收到,您的浏览器开发工具的网络选项卡会显示什么?有发帖请求吗?到哪个网址?等等……
  • @ducnguyendinhtrung 如果有任何从 UI 到 Action 的回发,您需要告诉我们。简单地说“不工作”是一个非常模糊的术语。
  • @Rahatur 当我点击编辑按钮(我想编辑我的名字)时,我看不到任何回发。

标签: c# asp.net asp.net-mvc asp.net-core


【解决方案1】:

FormCollection 用于列出您的表单只是一个名称和类别 ID 那么您的模型不合适请更改您的模型我的意思是创建包含名称和类别的模型类。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-28
    • 1970-01-01
    相关资源
    最近更新 更多