2

I'm having this AngularJs http.post-function. In my view I have a list of Id:s. And when I click on any of these Id:s this function gets hit.

$scope.GoToSinglePost= function (id) {
        console.log(id);
        if (confirm("Go to blogPost with " + id + " ?")) {

            $http.post("/Home/SingleBlogPost", { postId: id });

        }
    };

Everything works fine here and the post gets me to my mvc c# actionresult witch is this one below:

public ActionResult SingleBlogPost(int? postId)
    {
        var singlePost = _repo.GetSinglePost(postId);

        var viewModel = new SingleBlogPostViewModel()
        {
            Post = singlePost
        };

        return View(viewModel);
    }

In this ActionResult the postId i've sent from my angularPost gets in to the parameter int? postId And the actionResult starts to render and it's if I debug it, I can see that it will go in to its view (SingleBlogPost.cshtml)

But what happens if I watch it in my browser, everything stays the same. I'm still in the same view where I was from the beginning. Why doesn't my SingleBlogPost Actionresult-view renders in the browser? Why I'm I still on the same page even though the SingleBlogPost-method gets hit and goes thuogh it's view when I debug?

//Thanks

5
  • 2
    you have to add an event handler to do something with the data. angular doesn't automatically know what you want to do with the response. See "general usage" section here: docs.angularjs.org/api/ng/service/$http Commented Aug 24, 2014 at 19:49
  • Why are you using AngularJS with ASP.NET in the first place? AngularJS is meant to handle all the templating, so it makes little sense to use it with server-generated dynamic HTML. The Angular way would be to use ASP.NET only as business backend that generates data for Angular to render. WebApi is what you should use to complement Angular. Commented Aug 24, 2014 at 20:52
  • @crhis there are many reasons why you would use ASP.NET as your backend with angular, the whole story about decoupling, you already have a backend made, you want to share your backend with other apps, etc. in my work I already saw this solution for a financial business, so its something not so crazy after all Commented Aug 24, 2014 at 20:55
  • Maybe, but having an MVC framework both at the server AND the client side adds a lot of complexity. You should have really good reasons to justify that. If it's just about the data binding, there are more lightweight alternatives to Angular that don't try to be full blown MVC frameworks. Commented Aug 24, 2014 at 21:04
  • @chris I need to use the MVC framework in my project. What alternatives do you suggest to bind the data to my MVC-controller instead of using angularJs? The reason I have Angular in first place is that I render my list of Id:s trough an angularJs instant-search. I found the angular-instantSearch to be very useful Commented Aug 24, 2014 at 21:08

1 Answer 1

2

try adding a success or then once the operation is complete to perform further actions there, something like:

$http.post("/Home/SingleBlogPost", { postId: id }).success(function(data) {
    //data is your response from server to the request;
    //do something with data here, like changing to correct view;


});
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for your reply. I'm a newbie on angular. Can you please give me some futher advice how I should do?
Thank for your answer! I added window.location = "blogg/" + id; inside my $http.post and now everything works!
because is asynchronous, you have to wait to the request to be processed to be able to play with the response, thats the success or then, are promises(or alike)
glad to help, im also kind of newbie with angular, thats why im reading so angular questions ;)
You shouldn't directly set window.location in a controller, because that defeats testability - which is one of the main reasons to use AngularJS in the first place. Use the $location service instead. See docs.angularjs.org/guide/$location

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.