【问题标题】:Refresh table list using Ajax in Asp.Net Mvc在 Asp.Net Mvc 中使用 Ajax 刷新表列表
【发布时间】:2017-04-05 11:33:26
【问题描述】:

我的模式有问题,在我创建一个新条目后,我的表列表没有显示表中最后创建的行(在我的数据库中它已经存在),它仅在我刷新后才显示页。

我尝试了一些东西,但只在第一次工作(来自:Refresh table using AJAX in ASP.NET MVC)。

这是我的控制器代码:

public ActionResult IndexEvent()
    {
        return View(db.tbl_Event.ToList());
    }

    [HttpGet]
    public ActionResult Add()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Add(BOL3.tbl_Event eve)
    {
        if(ModelState.IsValid)
        {
            db.tbl_Event.Add(eve);
            db.SaveChanges();
        }
        return IndexEvent();
    }

,这里是动作按钮和模态:

<button type="button" class="btn btn-info btn-xs" data-toggle="modal" data-target="#enquirypopup">Add</button>

<div id="enquirypopup" class="modal fade in" role="dialog" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content row">
            <div class="modal-header custom-modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title">Add Event</h4>
            </div>
            <div class="modal-body">
                <form id="myForm">

                   <div class="modal-body">
                        <div class="row form-group">
                            <div class="col-md-12">
                                <div class="input-group">
                                    <span class="input-group-addon"></span>
                                    <input type="text" class="form-control" name="Event" id="Event" placeholder="Event Name">
                                </div>
                            </div>
                        </div>     
                        <div class="row form-group">
                            <div class="col-md-12">
                                <div class="input-group">
                                    <span class="input-group-addon"></span>
                                    <input type="text" class="form-control" name="Start_Date" id="Start_Date">
                                </div>
                            </div>
                        </div>                        
                        <div class="row form-group">
                            <div class="col-md-12">
                                <div class="input-group">
                                    <span class="input-group-addon"></span>
                                    <input type="text" class="form-control" name="End_Date" id="End_Date">
                                </div>
                            </div>
                        </div>                        
                        <br />
                        <div class="modal-footer">
                            <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
                            <button type="submit" class="btn btn-success" id="btnSubmit">Save</button>
                        </div>
                </form>
            </div>
        </div>
    </div>
</div>

这里是表格和脚本部分:

<script>
    $(document).ready(function () {
        $("#btnSubmit").click(function () {
            var myformdata = $("#myForm").serialize();

            $.ajax({
                type: "POST",
                url: "/Prog/Add",
                data: myformdata,
                success: function () {
                    $("#enquirypopup").modal("hide");
                    $("#tbl").load("/Prog/IndexEvent");
                    //$("#tbl").reload("/Prog/IndexEvent");
                }
            })

        })
    })
</script>
@model.....
<div class="table-responsive">
    <table class="table table-bordered table-striped" id="tbl">
        <thead>
            <tr>
                <th>Event Name</th>
                <th>Starting (Date and Time)</th>
                <th>Ending (Date and Time)</th>
                <th>All Day ?</th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            @foreach(var item in Model)
            {
                <tr>
                    <td>
                        @Html.DisplayFor(modelItem => item.Event)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Start_Date)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.End_Date)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.All_Day)
                    </td>
                    <td style="align-content:center">
                        
                        <a href="@Url.Action("EditEvent", "Prog", new { id = item.ID})" class="editDialog">Edit</a> |                       
                        @Html.ActionLink("Delete", "Delete", new { id = item.ID })
                    </td>
                </tr>
            }
        </tbody>
    </table>
</div>

我真的不知道这个模态究竟是如何工作的,这是我第一次使用它们,所以如果你能帮助我,我真的很感激它。谢谢。

【问题讨论】:

标签: jquery ajax asp.net-mvc entity-framework


【解决方案1】:

简单! 添加 location.reload() 成功。

     $.ajax({
             type: "POST",
             url: "/Prog/Add",
             data: myformdata,
             success: function () {
             $("#enquirypopup").modal("hide");
             location.reload();
              }
            });

希望有所帮助。

【讨论】:

  • 还是不行,谢谢你的回答。
  • 如果你想重新加载当前页面,那么它可以工作。
  • 我的目标是,当我从模态提交新插入的行时,能够在不刷新页面的情况下看到它。再次感谢。
  • 将控制器方法返回类型更新为 JsonResult
  • 添加 alert("result"); 调试你的成功方法;
猜你喜欢
  • 2016-12-02
  • 2011-03-16
  • 1970-01-01
  • 2018-12-11
  • 1970-01-01
  • 1970-01-01
  • 2020-08-09
  • 1970-01-01
  • 2010-09-22
相关资源
最近更新 更多