更新PartialView MVC 4 [英] Updating PartialView mvc 4
问题描述
安永!
我怎么能刷新与出模型的数据的局部视图?
第一次,当它工作正常,但页面加载时不我叫它从操作。
我创建了结构如下:
任何地方在我看来:
@ {Html.RenderAction(UpdatePoints);}
我PartialViewUpdatePoints
< H3>您的点是@ ViewBag.points< / H3 GT&;
在控制器我有:
公众的ActionResult UpdatePoints()
{ ViewBag.points = _Repository.Points;
返回PartialView(UpdatePoints);
}
感谢您的帮助!
更新
感谢所有您的帮助!最后,我用的JQuery / AJAX如你所说,使用模型传递参数。
所以,在JS:
$('#divPoints')负载('/计划/ UpdatePoints',UpdatePointsAction)。
VAR点= $('#newpoints)VAL()。
$ element.find('PointsDiv')的html(你+分+点)。
在控制器:
VAR模型= _newPoints;
返回PartialView(模型);
在View
< DIV ID =divPoints>< / DIV>
@ Html.Hidden(newpoints模型)
所以,说你有PartialView,这不得不通过点击按钮来更新您的视图:
< DIV CLASS =目标>
@ {Html.RenderAction(UpdatePoints);}
< / DIV><输入类=按钮值=更新/>
有一些方法来做到这一点。例如,你可以使用jQuery:
<脚本类型=文/ JavaScript的>
$(函数(){
$('按钮')点击(函数(){
.post的$('@ Url.Action(PostActionToUpdatePoints,家))。总是(函数(){
$('追求的目标')负载('/主页/ UpdatePoints')。
})
});
});
< / SCRIPT>
PostActionToUpdatePoints
是动作
与 [HttpPost]
属性,您可以用更新点
如果你在你的行动UpdatePoints使用逻辑()来更新点,也许你忘了添加[HttpPost]属性给它:
[HttpPost]
公众的ActionResult UpdatePoints()
{
ViewBag.points = _Repository.Points;
返回PartialView(UpdatePoints);
}
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 structure I've created looks like:
Anywhere in my View:
@{ Html.RenderAction("UpdatePoints");}
My PartialView "UpdatePoints":
<h3>Your points are @ViewBag.points </h3>
At the Controller I have:
public ActionResult UpdatePoints()
{
ViewBag.points = _Repository.Points;
return PartialView("UpdatePoints");
}
Thanks for your help!
UPDATE
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)
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')click(function(){
$.post('@Url.Action("PostActionToUpdatePoints", "Home")').always(function(){
$('.traget').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");
}
这篇关于更新PartialView MVC 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!