Quantcast
Channel: ASP.NET AJAX + Ajax Control Toolkit (ACT)
Viewing all articles
Browse latest Browse all 5678

ASP.NET MVC AJAX WebGrid Being Duplicated When Filtering

$
0
0

I'm working on paging, sorting, and filtering a WebGrid for the first time and I've run into a roadblock.  I have everything working but when I enter text into my search box to filter the WebGrid, I get a second WebGrid posted back to the page.  Can you help me understand why this is happening?

Here is my model:

public class ModelServices : IDisposable
    {
        private readonly Entities _entities = new Entities(HttpContext.Current.Application["connectionString"].ToString());
        public IEnumerable<Schedule> GetSchedules()
        {
            return _entities.Schedules.ToList();
        }

        public void Dispose()
        {
            _entities.Dispose();
        }
    }


Here is my controller:

public class ScheduleController : Controller
    {
        readonly ModelServices _mobjModel = new ModelServices();

        public ActionResult Index()
        {
            var schedules = _mobjModel.GetSchedules();
            return View(schedules);
        }

        [HttpPost]
        public ActionResult Index(string description)
        {
            var schedules = _mobjModel.GetSchedules();
            
            if (!string.IsNullOrEmpty(description))
                schedules = schedules.Where(s => s.Description.ToLower().Contains(description.ToLower())).ToList();

            return PartialView("_grid", schedules);
        }
    }

Here is my Index.cshtml:

@model IEnumerable<Schedule>
@using Scheduler
@using System.Globalization
@{
    ViewBag.Title = "Schedules";
    Layout = "~/Views/Shared/_Layout.cshtml";
}<script src="../../Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
@using (Ajax.BeginForm(new AjaxOptions 
    { HttpMethod = "Get", InsertionMode = InsertionMode.Replace, UpdateTargetId = "myGrid" }))
{<fieldset><legend>Schedules</legend><div>
            Description: <input type="text" id="description" name="description" /> <input type="submit" value="Search" /></div></fieldset>
}<div id="myGrid">
    @Html.Partial("_grid", Model)</div><div>
    Loaded at @DateTime.Now.ToString(CultureInfo.InvariantCulture)</div>

And here is my _grid.cshtml:

@model IEnumerable<Schedule>
@using Scheduler

@{
    var grid = new WebGrid(Model, rowsPerPage: 5, ajaxUpdateContainerId: "grid");
    
    @grid.GetHtml(htmlAttributes: new { id = "grid" },
                  fillEmptyRows: false,
                  alternatingRowStyle: "alternate-row",
                  headerStyle: "grid-header",
                  footerStyle: "grid-footer",
                  mode: WebGridPagerModes.All,
                  firstText: "<< First",
                  previousText: "< Prev",
                  nextText: "Next >",
                  lastText: "Last >>",
        columns: new[] {
        grid.Column("Description", style: "description"),
        grid.Column("ScheduleType", "Type", style: "scheduletype"),
        grid.Column("EnableDate", "Enable Date", s=>s.EnableDate.ToShortDateString(), style: "enabledate"),
        grid.Column("DisableDate", "Disable Date", s=>s.DisableDate != null ? s.DisableDate.ToShortDateString() : "", style: "disabledate"),
        grid.Column("", "", 
                @<text>
                    @(Html.ActionLink("Edit", "Edit", new { id = item.ScheduleId }, new { @class = "actionlink" }))
                    |
                    @(Html.ActionLink("Delete", "Delete", new { id = item.ScheduleId }, new { @class = "actionlink" }))</text>)
        })
}






Viewing all articles
Browse latest Browse all 5678

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>