我有按钮.我点击按钮时想要路由新视图.按钮如下:
<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(); }
解决方法
如果要刷新页面:
控制器:
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() },success: function (data) { $("#divContent").html(data); } }); }); </script>
TestAjax.cshtml:
@ViewBag.Name