从 Asp.net 视图页面返回带有 Ajax 调用的视图的调用控制器方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39523270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 10:31:08  来源:igfitidea点击:

Call Controller Method Which Return View With Ajax Call From Asp.net View Page

ajaxasp.net-mvcviewcontrollerroutes

提问by Hymanson Casper

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 ?

当我调试我的程序时,流程来到我的控制器动作。但是索引网页不会路由到测试视图页面。没有发生错误。我能为这个州做什么?

采纳答案by Sandip - Full Stack Developer

If you wants to refresh page:

如果要刷新页面:

Controller:

控制器:

public ActionResult Index()
{            
    return View();
}

public ViewResult Test()
{
    ViewBag.Name = Request["txtName"];
    return View();
}

Index.cshtml:

索引.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:

测试.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:

索引.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:

TestAjax.cshtml:

@ViewBag.Name