更新 PartialView mvc 4 [英] Updating PartialView mvc 4

查看:19
本文介绍了更新 PartialView mvc 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我视野中的任何地方:

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

我的局部视图更新点":

你的积分是@ViewBag.points </h3>

在控制器我有:

public ActionResult UpdatePoints(){ViewBag.points = _Repository.Points;返回 PartialView("更新点");}

感谢您的帮助!

更新

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

所以,在 JS 中:

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

在控制器中:

var 模型 = _newPoints;返回 PartialView(模型);

在视图中

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

解决方案

因此,假设您的 View 带有 PartialView,必须通过单击按钮进行更新:

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

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

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

PostActionToUpdatePoints 是带有 [HttpPost] 属性的 Action,用于更新点

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

[HttpPost]公共 ActionResult 更新点(){ViewBag.points = _Repository.Points;返回 PartialView("更新点");}

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

这篇关于更新 PartialView mvc 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆