Jquery 在ASP MVC中使用控制器操作更新div而不重新充电页面

Jquery 在ASP MVC中使用控制器操作更新div而不重新充电页面,jquery,asp.net-mvc-3,razor,controller,Jquery,Asp.net Mvc 3,Razor,Controller,我的控制器中有此操作 public ActionResult TestItemLogistic() { ControlViewModel model = new ControlViewModel(); model.itemSelected = "Logistic"; return RedirectToAction("MenuList", model); } 当我单击一个按钮时,将调用此操作 <a href=@url.Act

我的控制器中有此操作

public ActionResult TestItemLogistic()
    {
        ControlViewModel model = new ControlViewModel();
        model.itemSelected = "Logistic";
        return RedirectToAction("MenuList", model);

    }
当我单击一个按钮时,将调用此操作

<a href=@url.Action(subItem.action, subItem.controller)>

我希望能够做到这一切,而不充电的网页,只是更新一些div代替

有什么想法吗

谢谢。

您可以使用从URL检索的HTML输出替换所选元素的内容

说你有

<a href="@url.Action(subItem.action, subItem.controller)" id="ClickMe">Click Me</a>
<div id="ReplaceMe"></div>

你可以这样做:

<script type="text/javascript">
 $(function() {
    $('#ClickMe').click(function(e) {
       e.preventDefault();
       $('#ReplaceMe').load($(this).attr('href'));
    });
 });
</script>

$(函数(){
$('#ClickMe')。单击(函数(e){
e、 预防默认值();
$('#ReplaceMe').load($(this.attr('href'));
});
});
编辑:我现在明白了,您需要一个指向某个操作的链接,该链接返回一个URL,然后您需要加载结果URL的内容

public ActionResult TestItemLogistic()
{
    ControlViewModel model = new ControlViewModel();
    model.itemSelected = "Logistic";

    UrlHelper u = new UrlHelper(ControllerContext.RequestContext);
    return Content(u.Action("MenuList", model));

}


<script type="text/javascript">
 $(function() {
    $('#ClickMe').click(function(e) {
       e.preventDefault();
       $.get($(this).attr('href'), function(url) {
              $('#ReplaceMe').load(url);
       });
    });
 });
</script>
public ActionResult TestItemLogistic()
{
ControlViewModel模型=新的ControlViewModel();
model.itemSelected=“Logistic”;
UrlHelper u=新的UrlHelper(ControllerContext.RequestContext);
返回内容(u.Action(“菜单列表”,模型));
}
$(函数(){
$('#ClickMe')。单击(函数(e){
e、 预防默认值();
$.get($(this.attr('href'),函数(url){
$('#ReplaceMe').load(url);
});
});
});

您可以通过将Ajax与jQuery结合使用来实现这一点
上面是jquery ajax的文档,您可以从视图中使用ajax属性。您可以添加一个链接,该链接可以按id更新任何元素

@Ajax.RouteLink("TestLink", new { controller = "Persoon", action = "Partial" },new AjaxOptions() { InsertionMode = System.Web.Mvc.Ajax.InsertionMode.Replace UpdateTargetId = "Id Element"})

如果您想使用AJAX,请尝试此操作。具体地说,我要看一看。谢谢你的回复。这似乎是一个很好的解决方案,我忘了告诉你,动作呈现的视图是相同的视图。因此,理想情况下,我希望在不返回任何内容的情况下执行操作,并从javascript更新div