C# 更新 PartialView mvc 4
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18254749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Updating PartialView mvc 4
提问by Mario Levrero
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:
哎!如何使用模型中的数据刷新局部视图?第一次,当页面加载时它工作正常,但不是当我从 Action 调用它时。我创建的结构如下所示:
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.
感谢你的帮助!最后我按照你的建议使用了 JQuery/AJAX,使用模型传递参数。
So, in JS:
所以,在 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)
采纳答案by Andriy Gubal
So, say you have your View with PartialView, which have to be updated by button click:
因此,假设您的视图带有 PartialView,必须通过单击按钮进行更新:
<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:
有一些方法可以做到。例如,您可以使用 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
PostActionToUpdatePoints
是你的Action
with[HttpPost]
属性,你用它来更新点
If you use logic in your action UpdatePoints() to update points, maybe you forgot to add [HttpPost] attribute to it:
如果您在操作 UpdatePoints() 中使用逻辑来更新点,则可能您忘记向其添加 [HttpPost] 属性:
[HttpPost]
public ActionResult UpdatePoints()
{
ViewBag.points = _Repository.Points;
return PartialView("UpdatePoints");
}
回答by Mario Levrero
Thanks all for your help! Finally I used JQuery/AJAX as you suggested, passing the parameter using model.
感谢你的帮助!最后我按照你的建议使用了 JQuery/AJAX,使用模型传递参数。
So, in JS:
所以,在 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)
回答by Rashad Annara
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.
它首先调用加载 div,然后以 30 秒的间隔进行后续调用。
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);
}
}
回答by Husam Ebish
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"
}
});
});
});