【发布时间】:2017-01-24 04:44:45
【问题描述】:
我有按钮。当我单击按钮时,我想路由新视图。按钮如下:
<button type="button" id="btnSearch" class="btn btn-warning" style="height:35px;width:120px"> <i class="fa fa-search" aria-hidden="true"></i> <translate>Search</translate> </button>
当按钮被点击并且下面的方法运行时:
$('#btnSearch').click(function () {
return $.ajax({
url: '@Url.Action("test", "ControllerName")',
data: { Name: $('#Name').val() },
type: 'POST',
dataType: 'html'
});
});
我的控制器动作如下:
public ActionResult test(string CityName) {
ViewBag.CityName = CityName;
return View();
}
当我调试我的程序时,流程来到我的控制器操作。但索引网页不会路由到测试视图页面。没有发生错误。我能为这个状态做些什么?
【问题讨论】:
-
ajax 的全部目的是保持在同一页面上。如果要在 POST 方法中重定向,则不要使用 ajax。或者,如果您要添加在
test()方法中返回的视图,则处理success回调并更新 DOM(尽管在这种情况下ViewBag.CityName = CityName;毫无意义) - 例如success: function(response) { $(someElement).html(response); }
标签: ajax asp.net-mvc view controller routes