Do I sent two requests to the ActionResult?

redirecttoaction
unit testing web api controllers using moq
unit test mvc controller actionresult
how to use tempdata in mvc view
iactionresult
asp.net core route attribute
tempdata in mvc example
pass multiple parameters in url mvc

I have an ASP.net MVC project and depending on the filter options chosen by the user I am sending different ajax requests to the same actionresult, for example:

 $(document).on("click", "#filter_reset_button", function () {
    var url = "/Admin/Index";

    ajaxRequest({
        url: url,
        type: "get",
        data: { reset: true },
        successCallback: function () {
            window.location.href = url;
        }
    });
});

Other listeners sent different data, something like:

 data: { page: 2, filterUpdate: true }

and so on. The Index ActionResult returns different lists of items, depending on different options chosen in the data and the code works completely fine.

A colleage of mine told me, that my code is actually sending two get requests to the AR everytime, so its not efficient. Is that true? And if its the case, how can I refactor it. to make it just one request? If I let window.location.href = url part out, the site actually doesnt load the server response.

Yes you are doing 2 request in button click. First in Ajax Get, Second in Success Call Back.

But Why are you calling window.location.href = url; success call back. ?

If you want update the page after click, you can do partial updates to page. Check this post.

TempData in ASP.NET MVC, TempData will be cleared out after the completion of subsequent request. return View(); } public ActionResult About() { string userName; int userAge; if( TempData. data in TempData in the first request and in the second subsequent request� I have an ASP.net MVC project and depending on the filter options chosen by the user I am sending different ajax requests to the same actionresult, for example: $(document).on("click", "#

That is correct 2 request called.

First request when you call AJAX get to Action Index in Admin Controller.

Second request when you set window.location.href = url, it will same as you enter /Admin/Index in browser.

In this case you only need window.location.href = '/admin/index?reset=true' in click function

Handle requests with controllers in ASP.NET Core MVC, If multiple controller actions require the same service, consider using constructor injection to request The action result does the responding. The ActionResult method is derived from the ActionResult Class. The ActionResult Class encloses the output of the action method and does the operation. ActionResult is a very valuable aspect of the MVC. ActionResult. The ActionResult method works as a return type of any controller method in the MVC. It acts like the base class of Result classes.

You can see the post here at this post

Actually on success callback you must change your code accordingly to the above post

Test controller logic in ASP.NET Core, IsValid is false , the action method returns a 400 Bad Request binding isn't running (although an integration test does use model binding). NET Core 2.1 or later, ActionResult<T> (ActionResult<TValue>) Two tests of the ForSessionActionResult controller are included in the ApiIdeasControllerTests . To get a deeper look at what exactly an ActionResult type is and how it works, running the code with a breakpoint set at the end of an action result method will get the information we want. Inspecting the image below of the Watch window shows some of the properties that you can tap into that ActionResults return.

Url Routing in Asp.Net MVC Example with Multiple Parameters , Asp.net mvc url routing example with multiple parameters. In asp.net mvc application whenever user send request and if it is a first request then route table will fill For Adding View just right click inside Index ActionResult Method and Select� public ActionResult Index() { return View(); } Any action in an MVC controller, generally speaking, returns an ActionResult. Obviously that class represents an action of some kind, but exactly what does it do? Let's peel back the covers on ActionResult and discover why it and its derived classes are critical to understanding ASP.NET MVC.

Action Result In ASP.NET MVC, So, it has to be abstract so that every HTTP request could handle properly. As you can see, the same action method “Index” is returning two� When it is used with action method, it is called return type. As you know, an action is referred to as a method of the controller, the Action Result is the result of action when it executes. In fact, Action Result is a return type. This return type has many other derived types. First, look at the base and derived types of ActionResult.

IActionResult and ActionResult, It is the process by which the server determines the format in which to send the response. When the server detects an Accept header in the request, it will enumerate Arguably the simplest kind of action result that is defined by ASP. and end up at the same Target action as the previous two examples: Creating a custom ActionResult To create a custom action result class, all you need to do is, create a class that implements the IActionResult interface and overrides the ExecuteAsync method.

Comments
  • Add the sample of Index Action method so others may help you further.
  • So is there a way to improve my code? Because without the window.location.href my browser doesn`t load the server response into the window.
  • you dont need ajax in this case, you can only use /admin/index?reset=true
  • It sadly wouldn`t work, we have some complex logic in the Index AR and reset is one of many parameters, so basically if I do /admin/index?reset=true it deletes another options which were already saved, but if I do Ajax request and windows.location.href after that, reset gets saved into the AR and the new page loads with all the previously saved options (reset = true included).
  • You need return data in result ajax and update UI base on json data
  • Could you explain it a little bit more in depth? I am not sure I understand how to do what you have written there.