更新数据后刷新Grid.Mvc

时间:2015-04-22 15:48:48

标签: jquery ajax model-view-controller grid

我有这样的Grid Mvc:

    @helper CustomRendering(int id)
{
    @*<button class="btn btn-primary" onclick="location.href='@Url.Action("AcceptRequest", "TableRequest", new { id = id })'"> Accept</button>*@
    <button class="btn btn-primary" onclick="postAccept(@id)" id="@id"> Accept</button>
    <button class="btn btn-danger" onclick="setWindowId(@id)" id="@id">Decline</button>
}
@Html.Grid(Model).Columns(columns =>
                    {                                                
                        columns.Add(c => c.UserName).Titled("Name").Filterable(true);
                        columns.Add(c => c.DateStart).Titled("DateStart");
                        columns.Add(c => c.DateEnd).Titled("DateEnd");
                        columns.Add(c => c.Approved).Titled("Approved");
                        columns.Add(o => o.Id).Encoded(false).Sanitized(false)
                                    .Titled("Action")
                                    .RenderValueAs(o => CustomRendering(o.Id).ToHtmlString());                                           
                    }).WithPaging(10).Sortable(true)

我有这样的js脚本:

var tempId;        
        function setWindowId(id) {
            $("#dialog").dialog();
            tempId = id;
        }       
        function postMessage() {        
            var message = $('textarea#commentForDecline').val();
            var obj = {};
            obj.mes = $('textarea#commentForDecline').val();
            obj.mes = message;
            obj.id = tempId;

            $.ajax({
                type: "POST",
                url: "/TableRequest/DeclineRequest",            
                data: {'message': obj.mes, 'id': obj.id},
                success: function (msg) {                    
                }
            });       
            $("#dialog").dialog('close');
            $('textarea#commentForDecline').val('');
        }

        function postAccept(id){
            $.ajax({
                type: "POST",
                url: "/TableRequest/AcceptRequest",
                data: {'id': id },
                success: function (msg) {
                }
            });
        }

正如您可以看到我用于按钮的这些js函数,您可以在块@helper上看到的内容。我只是将两个post ajax调用发送到MVC操作中。我遇到了一个问题:刷新页面以查看任何更新都是必要的。 DB更新后有没有办法刷新Grid MVC?

public virtual ActionResult AcceptRequest(int id)
        {           
            using(var _db = new ApplicationDbContext())
            {
                Shedule shedule = _db.Shedules.SingleOrDefault(x => x.Id == id);
                shedule.IsDirectorApproved = true;
                _db.SaveChanges();
            }
            return RedirectToAction("Index");
        }

        [HttpPost]
        public virtual ActionResult DeclineRequest(string message, int id)
        {
            using (var _db = new ApplicationDbContext())
            {
                Shedule shedule = _db.Shedules.SingleOrDefault(x => x.Id == id);
                shedule.IsDirectorApproved = false;
                shedule.Message = message;
                _db.SaveChanges();
            }
            return RedirectToAction(MVC.TableRequest.ActionNames.Index);
        }

0 个答案:

没有答案