2

I am using MVCGrid.NET http://mvcgrid.net/

And I created a Non-Fluent grid http://mvcgrid.net/gettingstarted see Non-Fluent Example

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);

Now I have my grid appear when you submit a form, but when I submit the form again, I am expecting new data, but the grid does not reload or refresh or anything. It does't even reset when I refresh the page, I have to do a full reload of the page to reset it, which is lame, does anyone know how to refresh or reload the grid when I want to show new data?

I have even tried this: http://mvcgrid.net/demo/NoQueryOnPageLoad

But it did not reload or refresh.

PLEASE HELP!

user979331
  • 11,039
  • 73
  • 223
  • 418

1 Answers1

2

It reloads, try this,

 //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

<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>

It adds item and reloads after "Submit". Just have installed MVCGrid in new asp.net mvc - Install-Package MVCGrid.Net Put these lines as above

Muthu R
  • 776
  • 1
  • 9
  • 15