0

I am using ASP.net MVC and AngularJS. In my current page I select some items and on button click the angular function is called

var itemIds = [];
 //... somehow I get my items from the grids
var url = 'http://localhost/myController/CompareItems';
   $http.post(url, groupIds)
                .then(function (response) {
//                    var data = response.data;
                    //what to do here?
                }
                ).finally(function () {
                    vm.loading = false;
                });
}

The c# MVC controller is like below: (myController.cs)

public async Task<IActionResult> CompareItems([FromBody] int[] itemIds)
        {
            var model = new CompareModel
            {
              property1= ..,
              property2= ..,
              ......
              ......

            };
            return View("CompareItems",model);

        }

On $http.post call, the debugger comes to the above controller, prepare the model and should return the View. There is no error but the page is not redirecting. What is wrong here?

N.B: I have searched many other posts before asking this question. I found lots of people are suggesting RedirectToAction. I tried this and that doesnot work. My question is, why should Redirect to another action. I am already in my expected action method with my required param values.

The thing is very simple, just collect the selected items from javascript and pass it to mvc controller-action. It is return a different view.

rasoo
  • 73
  • 1
  • 7
  • Another attempt I made: Changed the mvc controller class return type whic returns the view model. ``` public async Task CompareItems([FromBody] int[] itemIds) { var model = new CompareModel { property1= .., property2= .., ...... ...... }; return model; } ```And in http.post response, get the model. I know, window.location = 'new/url ' works. Question is how can I add the response model as parameter into window.location. – rasoo Jul 22 '21 at 15:20
  • 1
    Hi @rasoo. Just to clarify, with the first approach you have specified, you stated "I am already in my expected action method", does it mean that if you put a breakpoint on the action method, it is reached? Your call "enters" the action and then the view it's not returned? Or you can't even get to the action method? – Dave Miller Jul 23 '21 at 06:21
  • @DaveMiller , yes. by "expected method" I meant ```public async Task CompareItems([FromBody] int[] itemIds)``` Yes, on debug mode, if put breakpoint I can see the array values are correct and model is properly generated. But after executing the last line for returning the view, the state of the page in browser does not change. – rasoo Jul 23 '21 at 13:11

2 Answers2

1

Calling View from another folder

return View("MyViewFolder/MyViewName.cshtml");

Or with model as object

return View("MyViewFolder/MyViewName.cshtml", model);

Please also check you have set method return type as async which may prevent you if some ongoing execute are there with await.

Harshad Raval
  • 79
  • 2
  • 10
0

There appear to be something you missing.

You might want to replace

return View("Login")

with

return RedirectToAction("Login");

I am just giving an instance , you might want to replace it with what you have.

Emeka
  • 23
  • 2
  • Thanks for your response. I have already tried it. That doesnot work. I mean if I had to use like only "Login" that works. But I need the itemIds array, prepare a viewmodel and then return the view. – rasoo Jul 22 '21 at 11:41
  • How about looking up something like this ? Does this Answer you ? https://stackoverflow.com/questions/1257482/redirecttoaction-with-parameter – Emeka Jul 22 '21 at 11:47
  • And this too. https://stackoverflow.com/questions/1067200/how-to-add-querystring-values-with-redirecttoaction-method – Emeka Jul 22 '21 at 11:48