locked
How to refresh a page after update RRS feed

  • Question

  • User982203039 posted

    I have a view (index) with a partial view. I have created a standard update page  - but when I click edit on the index page - after I make a change and hit save it returns to the index page but I don't see my changes on the partial view until I hit refresh. How can I see these changes upon the page reloading from the save?

    Thanks!

    EB

    Thursday, October 10, 2019 2:02 AM

Answers

  • User2053451246 posted

    $.ajax({
                        type: "Get",
                        url: '/HelpDesk/ChangeDrop',
                        data: { Status: $("#Stat").val(), Tech: $("#Tech").val() },
                        dataType: "html",
                        success: function (data) {
                            $("#myPartialView").html(data); // HTML DOM replac
                        }
                    });

    Put this on the controller method for HelpDesk/ChangeDrop (that you didn't show us) to make sure it's not getting cached:

    [OutputCache(Duration = 0)]

    If that doesn't work google how to make sure your ajax request isn't caching anything.  I believe you have to put a setting in your call to prevent that, too.

    Also, put a break point at the ChangeDrop method and debug to make sure the break point is getting hit when it's supposed to be.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 10, 2019 5:03 PM

All replies

  • User665608656 posted

    Hi Baze,

    I have a view (index) with a partial view. I have created a standard update page  - but when I click edit on the index page - after I make a change and hit save it returns to the index page but I don't see my changes on the partial view until I hit refresh. How can I see these changes upon the page reloading from the save?

    Based on your description ,I created a functional page that is the same as what you need.

    Because I don't know your detailed code, I'm not sure why you didn't refresh the data, however you could refer to my example.

    In my example, I used bootstrap modal popup to display the part I was trying to edit data.

    At the same time, I use ajax to call edit method in controller :

    This is my controller:

    public class C_1010_2160513Controller : Controller
        {
            // GET: C_1010_2160513
            private EntityData db = new EntityData();
    
            public ActionResult Index()
            {
                return View(db.Personal_Details.ToList());
            } 
            
    
            public PartialViewResult Edit(int PersonID)
            {
                Personal_Details persontable = db.Personal_Details.Find(PersonID);
                return PartialView(persontable);
            }
    
        
            [HttpPost]
            public ActionResult Edit(Personal_Details persontable)
            {
                if (ModelState.IsValid)
                {
                    Personal_Details persontable_new = db.Personal_Details.Find(persontable.ID);
                    persontable_new.ItemName = persontable.ItemName;
                    persontable_new.Rate = persontable.Rate;
                    persontable_new.Supplier = persontable.Supplier;
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                return View(persontable);
            }
    
          
         
        }

    Index view:

    @model IEnumerable<MVC_Cases.Models.Personal_Details>
    @{
        Layout = null;
    }
    
    <!DOCTYPE html>
    
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Index</title>
        <link href="~/Content/bootstrap.min.css" rel="stylesheet" />
        <script src="~/Scripts/jquery-3.3.1.min.js"></script>
        <script src="~/Scripts/bootstrap.min.js"></script>
        <script type="text/javascript">
       $(function() {
        $('#editModal').modal();
          $(".edit").click(function(){
            event.preventDefault();
            $.ajax({
            url: '/C_1010_2160513/Edit?PersonID=' + $(this).attr("id"), // The method name + paramater
            success: function(data) {
                $('#modalWrapper').html(data); // This should be an empty div where you can inject your new html (the partial view)
                $('#editModal').modal("show");
            }
            });
    
            });
    });
    
        </script>
    </head>
    <body>
        <div id="main_div">
            <h2>Employees Data</h2>
            <table style="width:500px;text-align:center;border-collapse:collapse" border="1" class="table-primary">
                <tr>
                    <th>
                        @Html.DisplayNameFor(model => model.ID)
                    </th>
    
                    <th>
                        @Html.DisplayNameFor(model => model.ItemName)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.Rate)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.Supplier)
                    </th>
                    <th></th>
                </tr>
    
                @foreach (var item in Model)
                {
                    <tr>
                        <td>
                            @Html.DisplayFor(modelItem => item.ID)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.ItemName)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.Rate)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.Supplier)
                        </td>
                        <td>
                            <a  id="@item.ID" class="edit btn btn-primary"> Edit </a>
                        </td>
                    </tr>
                }
    
            </table>
    
    
        </div>
    
        <div id="modalWrapper">
        </div>
    </body>
    </html>
    

    Edit partial view:

    @model MVC_Cases.Models.Personal_Details
    
    @using (Html.BeginForm("Edit", "C_1010_2160513", FormMethod.Post))
    {
        <div class="modal fade" id="editModal">
            <div class="modal-dialog">
    
                <div class="modal-content">
                    <div class="modal-header">
                        <h4 class="modal-title">Edit</h4>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
    
                    </div>
                    <div class="modal-body">
                        <form>
                            <div class="editor-label">
                                @Html.LabelFor(model => model.ID):&nbsp;@Html.DisplayFor(model => model.ID) @Html.HiddenFor(model => model.ID)
                            </div>
                            <div class="editor-label">
                                @Html.LabelFor(model => model.ItemName)
                            </div>
                            <div class="editor-field">
                                @Html.EditorFor(model => model.ItemName)
                                @Html.ValidationMessageFor(model => model.ItemName)
                            </div>
    
    
    
                            <div class="editor-label">
                                @Html.LabelFor(model => model.Rate)
                            </div>
                            <div class="editor-field">
                                @Html.EditorFor(model => model.Rate)
                                @Html.ValidationMessageFor(model => model.Rate)
                            </div>
    
    
                            <div class="editor-label">
                                @Html.LabelFor(model => model.Supplier)
                            </div>
                            <div class="editor-field">
                                @Html.EditorFor(model => model.Supplier)
    
                                @Html.ValidationMessageFor(model => model.Supplier)
                            </div>
    
                        </form>
                    </div>
                    <div class="modal-footer">
    
    
                        <button type="button" class="btn btn-primary" data-dismiss="modal">Cancel</button>
                        <button type="submit" class="btn btn-primary">Save</button>
                    </div>
                </div>
            </div>
        </div>
    }
    

    Here is the result of this work demo:

    Best Regards,

    YongQing.

    Thursday, October 10, 2019 9:34 AM
  • User982203039 posted

    This looks great - but sorry I am confused as to my code. Here is where I am doing could you mock this up?

    Controller:

    public ActionResult Edit(int id)
            {
                var Branches = bd.Branches.Select(c => new {
                    BranchName = c.BranchName
                }).ToList();
    
                List<SelectListItem> Stat = new List<SelectListItem>()
                {
    
                    new SelectListItem { Text = "All", Value = "" },
                    new SelectListItem { Text = "Open", Value = "Open" },
                    new SelectListItem { Text = "New", Value = "New" },
                    new SelectListItem { Text = "In-Process", Value = "In-Process" },
                    new SelectListItem { Text = "Complete", Value = "Complete" }
                    
                };
    
                List<SelectListItem> Technician = new List<SelectListItem>()
                {
                    new SelectListItem { Text = "All", Value = "" },
                    new SelectListItem { Text = "None", Value = "None" },
                };
    
    
                ViewBag.Stat = Stat;
                ViewBag.Tech = Technician;
                ViewBag.Branches = new SelectList(Branches, "BranchName", "BranchName");
    
                var hd = db.HelpDesks.Find(id);
                if (hd == null)
                {
                    return HttpNotFound();
                }
                return View(hd);
            }
    
            [HttpPost]
            public ActionResult Edit(int id, HelpDesk model)
            {
                db.SaveChanges();
                return RedirectToAction("Index", new { id = id });
              
            }

    View:

    @model IEnumerable<PBTIntranet.Models.HelpDesk>
    
    @{
        ViewBag.Title = "HelpDesk";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    
        <style>
            .auto-style20 {
                font-size: 10pt;
                color: green;
            }
        </style>
    
    <!DOCTYPE html>
    
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Helpdesk</title>
        <style>
            .thumb {
                max-width: 100px;
                max-height: 100px;
                width: expression(this.width > 100 ? "100px" : true);
                height: expression(this.height > 100 ? "100px" : true);
            }
    
            .auto-style2 {
                color: green;
                font-size: 11px;
                white-space: nowrap;
            }
        </style>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        <script type="text/javascript">
    
    
            $(function () {
                $(".ddlfilter").change(function () {
                    $.ajax({
                        type: "Get",
                        url: '/HelpDesk/ChangeDrop',
                        data: { Status: $("#Stat").val(), Tech: $("#Tech").val() },
                        dataType: "html",
                        success: function (data) {
                            $("#myPartialView").html(data); // HTML DOM replac
                        }
                    });
    
                });
            })
    
        $(function () {
        $("#Stat").change();
                       });
    
        </script>
    
    
    
    </head>
    <body>
        <h2>Helpdesk  <span>@Html.ActionLink("Create New", "Create", null, new { @class = "auto-style20" })</span></h2>
        <h>
    
            Status: @Html.DropDownList("Stat", ViewBag.Stat as SelectList, new { @class = "ddlfilter" })
    
            Assigned: @Html.DropDownList("Tech", ViewBag.Tech as SelectList, new { @class = "ddlfilter" })
    
    
        </h><p>
    
        </p>
        <div id="myPartialView">
            @{Html.RenderPartial("HelpDeskDBList", Model);}
        </div>
    </body>
    </html>
    

    Partial View:

    @model IEnumerable<PBTIntranet.Models.HelpDesk>
    
    <style>
        .auto-style20 {
            font-size: 12pt;
            color: green;
        }
    </style>
    
    
    <table class="hoverTable">
        <tr>
            <th>
                Technician
            </th>
            <th>
                Request
            </th>
            <th>
                Status
            </th>
            <th>
                Requested By
            </th>
            <th>
                Branch
            </th>
            <th>
                Date
            </th>
    
            <th></th>
        </tr>
    
        @foreach (var item in Model)
        {
    <tr>
        <td style="width:200px;vertical-align:top;">
            @if (item.Status == "New")
            {
                @Html.ActionLink("Accept", "AssignHelpDeskTicket", "HelpDesk", new { id = item.ID }, new { @class = "auto-style20" })
            }
            @item.Technician
        </td>
        <td style="width:400px" valign="top">
            @Html.ActionLink(item.Request, "Details", "HelpDesk", new { id = item.ID }, null)
        </td>
        <td style="width:100px" valign="top">
            @item.Status
        </td>
        <td style="width:150px" valign="top">
            @item.RequestedBy
        </td>
        <td style="width:150px" valign="top">
            @item.Branch
        </td>
        <td style="width:250px" valign="top">
            @item.CreateDate
        </td>
        <td valign="top">
            @Html.ActionLink("Update", "Edit", new { id = item.ID }) |
            @*@Html.ActionLink("Details", "Details", new { id = item.ID }) |*@
            @Html.ActionLink("Delete", "Delete", new { id = item.ID })
        </td>
    </tr>
        }
    
    </table>
    

    Thursday, October 10, 2019 2:30 PM
  • User2053451246 posted

    $.ajax({
                        type: "Get",
                        url: '/HelpDesk/ChangeDrop',
                        data: { Status: $("#Stat").val(), Tech: $("#Tech").val() },
                        dataType: "html",
                        success: function (data) {
                            $("#myPartialView").html(data); // HTML DOM replac
                        }
                    });

    Put this on the controller method for HelpDesk/ChangeDrop (that you didn't show us) to make sure it's not getting cached:

    [OutputCache(Duration = 0)]

    If that doesn't work google how to make sure your ajax request isn't caching anything.  I believe you have to put a setting in your call to prevent that, too.

    Also, put a break point at the ChangeDrop method and debug to make sure the break point is getting hit when it's supposed to be.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 10, 2019 5:03 PM