Updating PartialView mvc 4

前端 未结 4 1816
广开言路
广开言路 2020-11-30 04:40

Ey! How I could refresh a Partial View with data out of the Model? First time, when the page loads it\'s working properly, but not when I call it from the Action. The struct

相关标签:
4条回答
  • 2020-11-30 05:25

    You can also try this.

     $(document).ready(function () {
                var url = "@(Html.Raw(Url.Action("ActionName", "ControllerName")))";
                $("#PartialViewDivId").load(url);
            setInterval(function () {
                var url = "@(Html.Raw(Url.Action("ActionName", "ControllerName")))";
                $("#PartialViewDivId").load(url);
            }, 30000); //Refreshes every 30 seconds
    
            $.ajaxSetup({ cache: false });  //Turn off caching
        });
    

    It makes an initial call to load the div, and then subsequent calls are on a 30 second interval.

    In the controller section you can update the object and pass the object to the partial view.

    public class ControllerName: Controller
    {
        public ActionResult ActionName()
        {
            .
            .   // code for update object
            .
            return PartialView("PartialViewName", updatedObject);
        }
    }
    
    0 讨论(0)
  • 2020-11-30 05:27

    So, say you have your View with PartialView, which have to be updated by button click:

    <div class="target">
        @{ Html.RenderAction("UpdatePoints");}
    </div>
    
    <input class="button" value="update" />
    

    There are some ways to do it. For example you may use jQuery:

    <script type="text/javascript">
        $(function(){    
            $('.button').on("click", function(){        
                $.post('@Url.Action("PostActionToUpdatePoints", "Home")').always(function(){
                    $('.target').load('/Home/UpdatePoints');        
                })        
            });
        });        
    </script>
    

    PostActionToUpdatePoints is your Action with [HttpPost] attribute, which you use to update points

    If you use logic in your action UpdatePoints() to update points, maybe you forgot to add [HttpPost] attribute to it:

    [HttpPost]
    public ActionResult UpdatePoints()
    {    
        ViewBag.points =  _Repository.Points;
        return PartialView("UpdatePoints");
    }
    
    0 讨论(0)
  • 2020-11-30 05:27

    Thanks all for your help! Finally I used JQuery/AJAX as you suggested, passing the parameter using model.

    So, in JS:

    $('#divPoints').load('/Schedule/UpdatePoints', UpdatePointsAction);
    var points= $('#newpoints').val();
    $element.find('PointsDiv').html("You have" + points+ " points");
    

    In Controller:

    var model = _newPoints;
    return PartialView(model);
    

    In View

    <div id="divPoints"></div>
    @Html.Hidden("newpoints", Model)
    
    0 讨论(0)
  • 2020-11-30 05:33

    Controller :

    public ActionResult Refresh(string ID)
        {
            DetailsViewModel vm = new DetailsViewModel();  // Model
            vm.productDetails = _product.GetproductDetails(ID); 
            /* "productDetails " is a property in "DetailsViewModel"
            "GetProductDetails" is a method in "Product" class
            "_product" is an interface of "Product" class */
    
            return PartialView("_Details", vm); // Details is a partial view
        }
    

    In yore index page you should to have refresh link :

         <a href="#" id="refreshItem">Refresh</a>
    

    This Script should be also in your index page:

    <script type="text/javascript">
    
        $(function () {
        $('a[id=refreshItem]:last').click(function (e) {
            e.preventDefault();
    
            var url = MVC.Url.action('Refresh', 'MyController', { itemId: '@(Model.itemProp.itemId )' }); // Refresh is an Action in controller, MyController is a controller name
    
            $.ajax({
                type: 'GET',
                url: url,
                cache: false,
                success: function (grid) {
                    $('#tabItemDetails').html(grid);
                    clientBehaviors.applyPlugins($("#tabProductDetails")); // "tabProductDetails" is an id of div in your "Details partial view"
                }
            });
        });
    });
    

    0 讨论(0)
提交回复
热议问题