MVCGrid.NET - 如何重新加载网格?

MVCGrid.NET - How to reload the grid?

我正在使用 MVCGrid.NET http://mvcgrid.net/

然后我创建了一个不流畅的网格http://mvcgrid.net/gettingstarted请参阅不流畅的示例

GridDefinition<YourModelItem> def = new GridDefinition<YourModelItem>();

GridColumn<YourModelItem> column = new GridColumn<YourModelItem>();
column.ColumnName = "UniqueColumnName";
column.HeaderText = "Any Header";
column.ValueExpression = (i, c) => i.YourProperty;
def.AddColumn(column);

def.RetrieveData = (options) =>
{
    return new QueryResult<YourModelItem>()
        {
            Items = new List<YourModelItem>(),
            TotalRecords = 0
        };
};

MVCGridDefinitionTable.Add("NonFluentUsageExample", def);

现在当您提交表单时我的网格出现了,但是当我再次提交表单时,我期待新数据,但网格没有重新加载或刷新或任何东西。当我刷新页面时它甚至没有重置,我必须完全重新加载页面才能重置它,这是蹩脚的,有谁知道当我想显示新数据时如何刷新或重新加载网格?

我什至试过这个:http://mvcgrid.net/demo/NoQueryOnPageLoad

但它没有重新加载或刷新。

请帮忙!

重新加载,试试这个,

 //model class
    public class YourModelItem
    {
        public int Id { get; set; }
        public string YourProperty { get; set; }
    }



    //controller
    public class HomeController : Controller
    {
        private static List<YourModelItem> _modelItems = new List<YourModelItem>();

        public ActionResult Index()
        {
            GridDefinition<YourModelItem> def = new GridDefinition<YourModelItem>();

            GridColumn<YourModelItem> column = new GridColumn<YourModelItem>();
            column.ColumnName = "UniqueColumnName";
            column.HeaderText = "Any Header";
            column.ValueExpression = (i, c) => i.YourProperty;
            def.AddColumn(column);

            def.RetrieveData = (options) => new QueryResult<YourModelItem>()
            {
                Items = _modelItems,
                TotalRecords = 0
            };

            MVCGridDefinitionTable.Add("NonFluentUsageExample", def);

            return View();
        }

        [HttpPost]
        public JsonResult Add(YourModelItem item)
        {
            _modelItems.Add(item);

            return Json(true, JsonRequestBehavior.AllowGet);
        }
    }

index.cshtml

@{
    ViewBag.Title = "Home Page";
}

@using MVCGrid.Web

<div class="jumbotron">
    <div id="form1">
        <div class="form-group">
            <label for="Id">ID</label>
            <input type="number" class="form-control" id="Id" aria-describedby="Id" placeholder="Enter Id">
        </div>
        <div class="form-group">
            <label for="YourProperty">YourProperty</label>
            <input type="text" class="form-control" id="YourProperty" placeholder="Enter Your Property">
        </div>

        <button id="addItem">Submit</button>
    </div>

    <br />

    @Html.MVCGrid("NonFluentUsageExample")
</div>

@section scripts {
    <script type="text/javascript">
        $(document).ready(function () {
            $("#addItem").click(function () {
                var formData = { Id: $('#Id').val(), YourProperty: $('#YourProperty').val() };

                $.ajax({
                    type: "POST",
                    url: "/Home/add",
                    dataType: 'json',
                    contentType: 'application/json',
                    data: JSON.stringify(formData),
                    success: function(data) {
                        if (data) {
                            MVCGrid.reloadGrid('NonFluentUsageExample');
                        }
                    }
                });
            });
        });
    </script>
}

_layout.cshtml -> 正文

<body>
    <div class="container body-content">
        @RenderBody()
    </div>

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    <script src="~/MVCGridHandler.axd/script.js"></script>

    @RenderSection("scripts", required: false)
</body>

它添加项目并在 "Submit" 之后重新加载。 刚刚在新 asp.net mvc - Install-Package MVCGrid.Net 中安装了 MVCGrid 把这些行放在上面