从 Asp.net 视图页面返回带有 Ajax 调用的视图的调用控制器方法 [英] Call Controller Method Which Return View With Ajax Call From Asp.net View Page
问题描述
我有按钮.我想在单击按钮时路由新视图.按钮如下:
当按钮被点击并且比下面的方法运行时:
$('#btnSearch').click(function () {返回 $.ajax({url: '@Url.Action("test", "ControllerName")',数据:{ 名称:$('#Name').val() },类型:'POST',数据类型:'html'});});
我的控制器操作如下:
public ActionResult test(string CityName) {ViewBag.CityName = CityName;返回视图();}
当我调试我的程序时,流程来到我的控制器动作.但是索引网页不会路由到测试视图页面.没有发生错误.我能为这个州做什么?
如果你想刷新页面:
控制器:
public ActionResult Index(){返回视图();}公共视图结果测试(){ViewBag.Name = Request["txtName"];返回视图();}
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>名称:</label><input type="text" id="txtName" name="txtName"/>}
Test.cshtml:
@ViewBag.Name
==============================================>
如果您不想刷新页面:
控制器:
public ActionResult Index(){返回视图();}[HttpPost]public PartialViewResult TestAjax(字符串名称){ViewBag.Name = 名称;返回 PartialView();}
Index.cshtml:
<label>名称:</label><input type="text" id="txtName" name="txtName"/><脚本>$('#btnSearch').click(function () {$.ajax({url: '@Url.Action("TestAjax", "Home")',数据:{ 名称:$("#txtName").val() },类型:'POST',成功:功能(数据){$("#divContent").html(数据);}});});
TestAjax.cshtml:
@ViewBag.Name
I have button. I want to route new view when i clicked the button. The button is like below:
<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>
when button is clicked and than the method which is below run:
$('#btnSearch').click(function () {
return $.ajax({
url: '@Url.Action("test", "ControllerName")',
data: { Name: $('#Name').val() },
type: 'POST',
dataType: 'html'
});
});
my controller action is below:
public ActionResult test(string CityName) {
ViewBag.CityName = CityName;
return View();
}
when i debug my program, flow came to my controller action. But index web page don't route to the test view page. Error is not occurred. What can i do for this state ?
If you wants to refresh page:
Controller:
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
=============================================
If you don't wants to refresh page:
Controller:
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
这篇关于从 Asp.net 视图页面返回带有 Ajax 调用的视图的调用控制器方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!