ASP MVC + AJAX,尝试异步更新div

我是Asp MVC的新手,我正在尝试完成一点异步更新(我使用的是MVC3 RC2,使用Razor,但我也可以使用ASPX解决方案)。 我有一个母版页面,通过调用Html.RenderAction(“购物车”,“商店”)在每个页面上呈现购物车盒。 ShopController的Cart动作调用数据库等,并输出结果。这有效。 第一个问题:如果我在这个局部视图中放置一个ActionLink(比如Html.ActionLink(“Remove”)),那么即使我从Remove动作中调用PartialView(),它也只渲染购物车,而不是其他任何东西(实质上) ,我的页面消失了)。 第二个问题:在这个局部视图中有一个名为“cartContent”的div。我希望能够在任何地方(不仅在主页面或部分视图中)放置一个链接,当按下时调用控制器Action,然后根据结果更新cartContent div。我尝试过Ajax.ActionLink,但它和Html.ActionLink一样,即使我导入了Microsoft.MvcAjax js libs。 另外,如果我解决了第一个问题,我希望它也是异步的。 我使用什么解决方案?我已经尝试将UpdateTargetID设置为“cartContent”,我已经尝试将cartContent包装成Ajax.BeginForm,没有。我是否必须使用jQuery(我不知道)?我是否序列化对JSON的一些响应,并在Javascript中手动更新div? (我不是很擅长JS,我来自C#方面)     
已邀请:
您可以在任何地方添加链接:
@Html.ActionLink("remove item", "remove", "somecontroller", 
    new { id = Model.Id }, new { @class = "remove" })
然后在一个单独的JavaScript文件中:
$(function() {
    $('.remove').click(function() {
        // when the link is clicked
        // perform an ajax request:
        $.ajax({
            url: this.href,
            type: 'delete',
            success: function(result) {
                // when the AJAX call succeed do something with the result
                // for example if the controller action returned a partial
                // then you could show this partial in some div
                $('#someDivId').html(result);
            }
        });

        // don't forget to cancel the default action by returning false
        return false;
    });
});
备注:如果您要更新的div也包含链接,那么您可能需要使用
.live()
函数,否则点击事件处理程序将无法再次运行,因为DOM将被修改:
$('.remove').live('click', function() {  
     ...
});
    

要回复问题请先登录注册