asp.net-mvc - MVC中如何刷新 View

标签 asp.net-mvc asp.net-mvc-3 asp.net-mvc-4

我有 _LayoutOnecs.html 文件, View 加载其中的渲染主体并显示表中的记录列表。在表列之一中,我有一个“删除”图标,单击它会转到 Controller 并从数据库中删除记录,一旦记录被删除, View 应该刷新,因此我将操作返回到获取所有记录的 Controller

public ActionResult GetAdvertisementDetails()
{
    var advertisementList = new AdvertisementManager().GetAdvertisementDetails();     
    return View("AdvertisementDetails", advertisementList);
}

public ActionResult DeleteAdvertisementDetails(int id)
{
    new AdvertisementManager().DeleteAdvertisementDetails(id);
    return RedirectToAction("GetAdvertisementDetails", "Advertisement");
}

一旦删除完成,它将转到 GetAdvertisementcontroller 并返回 View ,但已删除的记录位于表中,如果我按 F5 刷新页面,记录将从表中删除。 记录删除后如何自动刷新

查看代码

<div class="col-md-12 main_table">
    <div class="table-responsive">
        <table class="table" id="hom_table">
            <tr>
                <th>Advertisement  Name</th>
                 <th>Link</th>
                 <th>Start Date</th>
                 <th>End Date</th>
                 <th width="100">Action</th>
             </tr>
             @foreach (var advertisements in Model)
             {
                 <tr>
                     <td> @advertisements.Description</td>
                     <td> @advertisements.ImageUrl</td>
                     <td> @advertisements.StartDate</td>
                     <td> @advertisements.EndDate</td>
                     <td>
                         <a onclick="EditadvertisementDetails(@advertisements.AdvertisementId)">
                            <i class=" pull_Advt_details tbl_edit_btn fa fa-edit Editbutton"></i>
                         </a>
                         <a id="Deladvertisement" onclick="Deleteadvertisement(@advertisements.AdvertisementId)" >
                             <i class="tbl_del_btn fa fa-trash"></i>
                         </a>
                     </td>
                 </tr>
             }
         </table>
     </div>
     <!-- Responsive main table Ends -->
</div>

最佳答案

Ajax 调用保留在同一页面上。您在 Controller 方法中使用 return RedirectToAction("GetAdvertisementDetails", "Advertisement"); 将被忽略。也不需要重定向,因为您可以从表中删除该行

将 html 修改为(注意删除生成无效 html 的 id 属性):

<a class="delete" data-id="@advertisements.AdvertisementId>
  <i class="tbl_del_btn fa fa-trash"></i>
</a>

并使用以下脚本调用 Controller 方法并删除该行

var url = '@Url.Action("DeleteAdvertisementDetails", "Advertisement")';
$('.delete').click(function() {
  var row = $(this).closest('tr');
  $.post(url, { id: $(this).data('id') }, function(response) {
    if(response) {
      row.remove();
    }
  }).fail(function (response) {
    // display error message?
  });
});

并将 Controller 修改为

[HttpPost]
public JsonResult DeleteAdvertisementDetails(int id)
{
    new AdvertisementManager().DeleteAdvertisementDetails(id);
    return Json(true);
}

关于asp.net-mvc - MVC中如何刷新 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29513412/

相关文章:

asp.net - 在 ASP.NET MVC 中将对象的 JSON 数组发布到 Controller

asp.net-mvc - ASP.net MasterPageFile 属性是什么?

c# - 在 asp.net mvc3 中使用复选框加上批量更新

c# - MVC 4 获取选定单选按钮的值

Asp.net web api + Entity Framework : multiple requests cause data conflict

asp.net-mvc - 如何将数据从 View 模型传递给 javascript 函数?

c# - 即使它在那里也找不到 View ,它不是在寻找正确的扩展名

c# - 如何创建新的实体对象,在 SaveChanges() 之前在模型中修改它

javascript - 即使 javascript 验证失败,MVC 4 表单也会提交

c# - QueueClient.Receive() 的异步方法?