2011-01-28 72 views
0

我使用的MVCContrib主要用于分页,排序和过滤方面。我的网格包含我们的邮件列表应用程序中的所有地址(仅限后端)。用户可以删除,激活和禁用列表成员,所以在每行上都有这些选项的操作链接。使用jQuery,我捕获该点击并执行其他操作,例如完成操作的显示通知。执行行动后刷新MVCContrib网格

因此,我遇到的问题是在执行这些操作后如何刷新网格,以便用户可以看到结果?在删除时,我可以隐藏该行。当一个人激活/停用电子邮件时,我能做些什么?只需使用jQuery来更新表中显示状态的值?

// Activate the email address 
$(".ActivateLink").click(function() { 
    var id = $(this).attr("href"); 
    var i = id.indexOf("#"); 
    id = id.substring(i + 1); 
    $.ajaxSetup({ 'async': false }); 
    $.post("/List/Activate", { "id": id }, function(data) { 
     $(".message").text(data.message).addClass("notice"); 
    }, "json"); 
    return false; 
}); 

我的控制器操作:

// 
    // POST: /List/Activate 
    [HttpPost] 
    public ActionResult Activate(int id) 
    { 
     string message = String.Empty; 
     db.ActivateEventEmail(id, ref message); 
     return Json(new { message = message }); 
    } 

笔者认为:

<%= Html.Grid(Model.EmailAddressList).Columns(column => { 
    column.For("ImageActions").Named("").Sortable(false); 
    column.For(a => (a.Email.Length > 30) ? String.Format("{0}...", a.Email.Substring(0, 30)) : a.Email).Encode(true).SortColumnName("Email").Named("Email Address"); 
    column.For(a => (a.ContactName.Length > 30) ? String.Format("{0}...", a.ContactName.Substring(0, 30)) : a.ContactName).Encode(true).SortColumnName("ContactName").Named("Contact Name"); 
    column.For(a => a.SignupDate).Named("Signup Date").Format("{0:d}").Attributes(@style => "text-align: right;"); 
    column.For(a => a.AccountStatus ? "Yes" : "No").SortColumnName("AccountStatus").Named("Active").Attributes(@style => "text-align: center;"); 
}).Sort(Model.GridSortOptions) 
    .Attributes(@class => "table-list", style => "width: 100%;").RowAttributes(c => new MvcContrib.Hash(@class => "gridrow")) 
%> 

回答

0

好吧,我想通了这一点。

首先,我只是添加了一个函数,我将在后面的代码。

// Activate the email address 
$(".ActivateLink").live('click', function() { 
    var id = $(this).attr("href"); 
    var i = id.indexOf("#"); 
    id = id.substring(i + 1); 
    $.ajaxSetup({ 'async': false }); 
    $.post("/List/Activate", { "id": id }, function(data) { 
     refreshTable(); 
     $(".message").text(data.message).addClass("notice"); 
    }, "json"); 
    return false; 
}); 

上面的控制器动作,我不触及上面的视图,我没有触及。新的JavaScript函数创建如下:

function refreshTable() { 
     $.ajaxSetup({ 
      async: false, 
      cache: false 
     }); 
     $.get('<%= Url.Action("Index", "List", new { filterText = Model.FilterText, filterActive = Model.FilterActive, filterGroup = Model.FilterGroup }) %>', 
     function(response) { 
      $(".table-list").replaceWith(response) 
     }); 
    }; 

确保有cache: false在那里。 IE喜欢从缓存中提取数据,并且真正搞砸了一些东西。

在我的索引操作,我从return View(emailListGrid);改为

 if (Request.IsAjaxRequest()) 
      return PartialView("EmailMaint", emailListGrid); 
     else 
      return View(emailListGrid); 
+0

那么你发送整个电网背部和第四之间的客户端和服务器之间?除了我想在网格中添加一个新行外,我和你在同一条船上。所以我的“refreshTable”方法只是从响应中添加模型,而不是替换整个网格。 – Buzzer 2012-03-29 06:41:48