哎呀!
如何使用模型中的数据刷新部分视图?
第一次,当页面加载时它可以正常工作,但当我从操作中调用它时却不能正常工作。
我创建的结构如下所示:
在我看来任何地方:
@{ Html.RenderAction("UpdatePoints");}
我的部分观点“更新点”:
<h3>Your points are @ViewBag.points </h3>
在控制器我有:
public ActionResult UpdatePoints()
{
ViewBag.points = _Repository.Points;
return PartialView("UpdatePoints");
}
感谢您的帮助!
UPDATE
感谢你的帮助!最后我按照您的建议使用了 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);
In View
<div id="divPoints"></div>
@Html.Hidden("newpoints", Model)
因此,假设您有带有 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
with [HttpPost]
属性,用于更新点
如果您在操作 UpdatePoints() 中使用逻辑来更新点,则可能您忘记向其添加 [HttpPost] 属性:
[HttpPost]
public ActionResult UpdatePoints()
{
ViewBag.points = _Repository.Points;
return PartialView("UpdatePoints");
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)