更新部分视图mvc 4

时间:2013-08-15 14:23:10

标签: c# asp.net-mvc-4 partial-views viewbag

安永! 如何使用模型中的数据刷新部分视图? 第一次,当页面加载它正常工作时,但不是当我从Action调用它时。 我创建的结构如下:

我视野中的任何地方:

 @{ Html.RenderAction("UpdatePoints");}

我的PartialView“UpdatePoints”:

<h3>Your points are @ViewBag.points </h3>

在控制器我有:

public ActionResult UpdatePoints()
        {

            ViewBag.points =  _Repository.Points;
            return PartialView("UpdatePoints");
        }

感谢您的帮助!

更新

感谢大家的帮助!最后我按照你的建议使用JQuery / AJAX,使用模型传递参数。

所以,在JS:

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

在控制器中:

var model = _newPoints;
return PartialView(model);

在视图中

<div id="divPoints"></div>
@Html.Hidden("newpoints", Model)

4 个答案:

答案 0 :(得分:25)

所以,假设您有View with PartialView,必须通过按钮点击更新:

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

<input class="button" value="update" />

有一些方法可以做到这一点。例如,您可以使用jQuery:

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

PostActionToUpdatePoints是您的Action [HttpPost]属性,用于更新积分

如果您在动作UpdatePoints()中使用逻辑来更新点,可能您忘记为其添加[HttpPost]属性:

[HttpPost]
public ActionResult UpdatePoints()
{    
    ViewBag.points =  _Repository.Points;
    return PartialView("UpdatePoints");
}

答案 1 :(得分:7)

您也可以试试这个。

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

它进行初始调用以加载div,然后后续调用的间隔为30秒。

在控制器部分,您可以更新对象并将对象传递给局部视图。

public class ControllerName: Controller
{
    public ActionResult ActionName()
    {
        .
        .   // code for update object
        .
        return PartialView("PartialViewName", updatedObject);
    }
}

答案 2 :(得分:2)

感谢大家的帮助! 最后我按照你的建议使用JQuery / AJAX,使用模型传递参数。

所以,在JS:

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

在控制器中:

var model = _newPoints;
return PartialView(model);

在视图中

<div id="divPoints"></div>
@Html.Hidden("newpoints", Model)

答案 3 :(得分:0)

控制器:

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
    }

在以前的索引页面中,您应该具有刷新链接:

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

此脚本也应该在您的索引页中:

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