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

Jackson 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 ?

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:

@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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to call method of a controller from View without using ajax in ASP.NET MVC?

ASP.Net how to call a method in a controller without redirecting the view

In ASP.NET MVC: All possible ways to call Controller Action Method from a Razor View

Call controller method multiple times based on input in multiselect from view on submitting form [ASP.NET Core]

Call controller function from view! asp.net MVC

Call a controller method in a view

Simplest way to call a controller method from a view button in .Net (mvc)

Return list to ajax call from controller asp.net

Call controller from view and pass value from view to controller in asp.net mvc

Updating a view from an ajax controller call

Call app delegate method from view controller

Call method from controller in view (ROR)

Rails Ajax call from controller to view & update div inside view

JSON Call from view to controller and show JSON object data in view in ASP.NET Core 6 MVC

C# & ASP.NET MVC : call a view with ajax call

ASP.NET MVC - How do I Call a Controller Method from within a View to Redirect to Multiple Other Views?

call oracle view from asp.net?

ASP.NET MVC - How to call void controller method without leaving the view?

ASP.NET MVC - How to call controller method and don't leave the view

Check from which view method in controller got invoked ASP.NET MVC

Asp.net MVC Ajax call not calling controller method

ASP.NET Core - Call controller method in javascript without Ajax

Pass Model as a list from View to Controller using ajax call

get JSON object using jquery with ajax call from controller to view

Reading in view return values ​from the controller AJAX / ASP.NET Update NEW

Groovy/grails how to call controller method from view using jquery

How can I call a method in one view controller from another?

how to call an addRow view method from an ExtJS4 controller

How to call controller method from view RAILS 4.0