3

我目前正在开发一个具有 CRUD 功能的网站。在添加、编辑或删除成功完成后,我正在使用下面的 javascript 代码显示一条消息。

function addSuccess(data) {
    if (data.Success == true) {
        $('#addDialog').dialog('close');
        $('#commonMessage').html("Process Complete");
        $('#commonMessage').delay(400).slideDown(400).delay(3000).slideUp(400);
    }
    else {
        $("#add-message").html(data.ErrorMessage);
        $("#add-message").show();
    }
}

但是,我在包含 webGrid 的局部视图上呈现结果。在这种情况下,产品的表格列表位于此部分视图中。在(假设)编辑过程完成后,我希望编辑的字段在编辑对话框关闭后反映。我的问题是,我不知道如何在不影响整个页面的情况下刷新局部视图。

有人可以指出我这样做的好方法吗?非常感谢你!


编辑:

下面是我如何将项目添加到我的网格。请注意,整个代码都在我的 PartialView 中。谢谢!

@{
    MyStore.Business.Manager.ProductsManager repository = new MyStore.Business.Manager.ProductsManager ();

    List<MyStore.Data.Products> ProductsList = repository.GetAllProducts(ViewData["StoreCode"].ToString());

    var grid = new WebGrid(ProductsList );

    grid.Pager(WebGridPagerModes.All);
            @grid.GetHtml(tableStyle: "webGrid",
                htmlAttributes: new { id = "DataTable" },
                headerStyle: "header",
                alternatingRowStyle: "alt",
                columns: grid.Columns(
                    grid.Column("ProductCode", style: "width: 400px;"),
                    grid.Column("Name", style: "width: 400px"),
                    grid.Column("Price", style: "width: 100px;"),
                    grid.Column("", format: @<text>@Html.ActionLink("Edit", "_Edit", new { RecordID = item.RecordID }, new { @class = "editLink" })</text>, style: "width: 100px"),
                    grid.Column("", format: @<text>@Html.ActionLink("Delete", "_Delete", new { RecordID = item.RecordID }, new { @class = "editLink" })</text>, style: "width: 100px")
                                                 ));                                                      
    }            
4

1 回答 1

3

使用 jqueryajax重新加载内容

if (data.Success == true) {
    $('#addDialog').dialog('close');
    $('#commonMessage').html("Process Complete")
                   .delay(400).slideDown(400).delay(3000).slideUp(400);

    $("#yourContainerDiv").load("Url.Action("GetProducts ","Items")")

}

假设yourContainerDiv是您拥有网格标记的 Div /table 并且控制器的GetProducts操作方法Items返回网格的标记。

如果您在按钮单击事件中执行保存/更新,请确保使用peventDefault方法停止默认表单提交行为

$("#someButtonId").click(function(e){
  e.preventDefault();
  //Save data or whatever ...
}); 

编辑: 设置更新的问题后。

你为什么将你的 UI ( view) 与code. 保持分开。

获取action数据并将其传递给强类型视图

public ActionResult GetProducts()
{
   var repository = new MyStore.Business.Manager.ProductsManager ();
   var productList = repository.GetAllProducts(ViewData["StoreCode"].ToString());
   return View(productList);    
}

现在有一个 view( GetProducts.cshtml),它是 Lost of Products 类的强类型。

@model MyStore.Data.Products
@{
  Layout=null;
}
<table>
   @foreach(var item in Model)
   {
     <tr>
      <td>@item.ProductCode</td>
      <td>@item.Name</td>
      <td>@item.Price</td>
     </tr>
   }
</table>

就我个人而言,我将我的实体/模型名称保持为单数,例如Customer/Product

于 2012-07-16T02:25:41.687 回答