0

I have an angularjs MVC application which posts a form to a different domain and a call back comes to MVC controller function as plain http post. When the call back comes back and the view is loaded, it does not run the angularjs controller tied to the view. How can we load the angularjs and use the data in the controller? Thank you!

[System.Web.Http.HttpPost]
public ActionResult Callback()
{
   return View("CallbackView");
}

CallbackView.cshtml

<div ng-controller="CallbackCtrl">
</div>

<script>
    //How to load angularjs?
    //How to read the http post data and use it in the CallbackCtrl?
</script>

1 Answer 1

0

AngularJS will only load if the div where you bound your controller (or perhaps better, one of its parent elements) has the ng-app directive.

One approach to reading the post data is to serialize it to JSON in your controller method, then put it in a hidden input element, e.g.:

<input id="form-data" type="hidden" value="@model.MyFormData" />

From here you can simply use jQueryLite (included in AngularJS) to extract the value. This is certainly not the only way of doing this, just the method that immediately comes to mind.

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

3 Comments

I see the angularjs is loaded because the parent layout has ng-app. But how can I make it run the controller CallbackCtrl when the view is loaded? Can I directly read the post data in the CallbackCtrl ? Thank you!
As I suggested in one of your other question, Adding life cycle hooks to controller will help debug in depth. May be you controller is working and issue lies somewhere else.
You don't show a lot of your code here -- if the controller isn't working, it may simply be because you didn't actually name it CallbackCtrl. You can only read the post data if YOU explicitly put it somewhere in the ASP.NET MVC view template. That info doesn't automatically get inserted into the page like it did back in the web forms days. You can use the technique I suggested or a similar one to use the posted data in your controller.

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.