ASP.NET MVC 2:jQuery调用Action,更新View?

时间:2010-12-09 00:01:13

标签: jquery asp.net-mvc-2

控制器:

public ActionResult Index()
        {

            // not using the interface right now
            DealsRepository _repo = new DealsRepository();
            var deals = _repo.GetDeals("<Request><ZipCode>92618</ZipCode></Request>");

            return View(deals);
        }

        [HttpPost]
        public ActionResult Index(string data)
        {

            // not using the interface right now
            DealsRepository _repo = new DealsRepository();
            var deals = _repo.GetDeals(data);

            return View(deals);
        }

jQuery的:

var url = '<%: Url.Action("Index", "Deal") %>';
var data = '<Request><ZipCode>92618</ZipCode></Request>';

$.post(url, data, function (result) {
    alert(result);
});

我要做的是使用更新的模型刷新(重新加载)View ...我不想使用jQuery来更新内容。使用更新的模型重新查看视图。

换句话说,一旦用户在搜索中输入一些优化或选择过滤器,我就需要进行调用,并使用最新的模型搜索结果重新加载页面。

4 个答案:

答案 0 :(得分:2)

您根本不需要使用javascript来发布表单,这就是您正在尝试做的事情。

只需在html表单中添加一个提交按钮即可。单击提交按钮时,表单将发布到控制器和与html表单关联的操作。该控制器和操作将返回一个ActionResult,如果您选择的话,它将是您刷新的视图...

在您看来,您可以这样做:

<%Html.BeginForm("ActionName", "ControllerName", FormMethod.Post); %>
<!-- more form fields here -->
<input type="submit" value="Go"/>
<%Html.EndForm(); %>

答案 1 :(得分:0)

视图应该有一个带有id的div或类似对象(例如myDivId),然后在你的帖子中,你只需要做

$.post(url, data, function (result) {
     $('#myDivId').html(result);
});

答案 2 :(得分:0)

有时我只是在点击提交按钮后重新加载页面。

<script type="text/javascript">

    $("#SubmitButton").click(function () {
            window.location.reload(true); 
    }); 

</script>

答案 3 :(得分:0)

如果您只是想刷新页面,那么除非您在1-30秒范围内等待定时事件,否则JS AJAX调用的优势非常小

如果是这种情况,请等到你得到回复并简单地在JS中重新加载:

window.location.reload()

history.go(0)

(我推荐前者)