我有按钮。我点击按钮时想要路由新视图。按钮如下所示:
<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();
}
当我调试我的程序时,流程进入我的控制器操作。但索引网页不会路由到测试视图页面。没有发生错误。我能为这个州做些什么?
答案 0 :(得分:3)
如果您想刷新页面:
<强>控制器:强>
public ActionResult Index()
{
return View();
}
public ViewResult Test()
{
ViewBag.Name = Request["txtName"];
return View();
}
<强> Index.cshtml:强>
@using (Html.BeginForm("Test", "Home", FormMethod.Post ))
{
<input type="submit" id="btnSearch" class="btn btn-warning" style="height:35px;width:120px" value="Search"/>
<label>Name:</label><input type="text" id="txtName" name="txtName" />
}
<强> Test.cshtml:强>
@ViewBag.Name
=============================================
如果您不想刷新页面:
<强>控制器:强>
public ActionResult Index()
{
return View();
}
[HttpPost]
public PartialViewResult TestAjax(string Name)
{
ViewBag.Name = Name;
return PartialView();
}
<强> Index.cshtml:强>
<input type="button" id="btnSearch" class="btn btn-warning" style="height:35px;width:120px" value="Search"/>
<label>Name:</label><input type="text" id="txtName" name="txtName" />
<script>
$('#btnSearch').click(function () {
$.ajax({
url: '@Url.Action("TestAjax", "Home")',
data: { Name: $("#txtName").val() },
type: 'POST',
success: function (data) {
$("#divContent").html(data);
}
});
});
</script>
<强> TestAjax.cshtml:强>
@ViewBag.Name