Refresh Grid.Mvc data after refresh data

I have a Grid Mvc like this:

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

      

And I have a js script like this:

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

      

As you can see this js function I am using for buttons, which you can see on the block @helper

. I am just sending two ajax call messages to MVC actions. And I have a problem: the page needs to be refreshed to see any updates. Is there a way to update the Grid MVC after updating the DB?

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

      

+3


source to share





All Articles