1

I am passing url from view to controller for $http.Post.

View(Asp.net MVC View)

<form  name="form" ng-controller="LoginController" >
    <div id="dvContainer" ng-init="Url= @Url.Action("VerifyLogin", "Visitor")">
        <span ng-model="Message" class="text-danger"></span>
        <table>
            <tr>
                <td>UserName</td>
                <td>
                    <input name="txtUser" type="text" required ng-model="UserName" />
                    <span ng-show="(form.txtUser.$dirty || submitted) &&
                               form.txtUser.$error.required">UserName is required.</span>
                </td>
            </tr>
            <tr>
                <td>Password</td>
                <td><input type="password" required ng-model="Password" /></td>
            </tr>
            <tr>
                <td colspan="2"><button type="submit" ng-click="submit($event)">Login</button></td>
            </tr>
        </table>

    </div>
</form>

In my Controller(Angularjs Controller)

var app = angular.module('VisitorApp', []);

//AngularJS controller
app.controller('LoginController', function ($scope, $http) {
    $scope.submit = function () {
        alert('hi')
        // Set the 'submitted' flag to true
        alert($scope.UserName);
        alert($scope.Password);
        var loginModel = {
            UserName: $scope.UserName,
            PassWord:$scope.Password
        };

        $http.post($scope.Url, loginModel).success(function (data) {
            $scope.Message = data.Message;
        }).error(function (data) {
            $scope.error = "An error has occured while adding! " + data;
        });
    };
});

I am trying to access Url that is being defined using ng-init . The application is throwing error

[$parse:syntax] Syntax Error: Token '/' not a primary expression at column 6 of the expression [Url= /Visitor/VerifyLogin] starting at [/Visitor/VerifyLogin].

I am pretty new to Angularjs so Could not figure out what is that i am doing wrong.

3
  • Maybe there is an extra / at the start or end?? Commented May 6, 2016 at 15:40
  • Where/how did $scope.Url get defined? Commented May 6, 2016 at 15:46
  • @KKKKKKKK it is defined here ng-init="Url= @Url.Action("VerifyLogin", "Visitor")" Commented May 6, 2016 at 15:56

2 Answers 2

3

You can use the angular value provider to pass this url ( or any other stuff)from your view to your angular controller.

Just create a javascript object and create a Url property and set the value of this and use the value provider to pass this object.

@section Scripts
{
   <script src="~/Scripts/YourAngularControllerFileForThisPage.js"></script>
   <script>
        var myApp = myApp || {};
        myApp.Settings = myApp.Settings || {};
        myApp.Settings.Url= "@Url.Action("VerifyLogin","Visitor")";
        angular.module("app").value("appSettings", myApp);
   </script>
}

Now in your controller, simply use the appSettings object

var app = angular.module("app", []);
app.controller('ctrl', ['$scope', 'appSettings', function ($scope, appSettings) {
    $scope.URL = 'nothing';
    $scope.greeting = 'Hola!';
    console.log('Url  ', appSettings.Settings.Url);
    // You can use appSettings.Settings.Url for your ajax calls now
}]);
Sign up to request clarification or add additional context in comments.

Comments

0

I have been experiencing the same issue, and didn't want to use value to pass the Url.Action to my controller. I found out that adding an apostrophe at the beginning and end of the Url.Action's returned value solves the issue. I ended up writing this extension to help me:

  public static MvcHtmlString ActionString(this UrlHelper url, string action, string controller, object routeValues)
    {
        return MvcHtmlString.Create("'" + url.Action(action, controller, routeValues) + "'");
    }

Comments

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.