2015-04-05 56 views
0

我是新來的角js。我試圖將表單數據傳遞給操作,但它不起作用,它不會給我任何錯誤。請幫幫我。http post不能調用ASP.NET MVC動作

下面

是我的js文件

var aboutmeApp = angular.module('aboutmeApp', ['ui.router']); 

var configFunction = function ($stateProvider, $httpProvider, $locationProvider) { 
    $locationProvider.hashPrefix('!').html5Mode(true); 

    $stateProvider 
    .state('Default', { 
     url: '/', 
     views: { 
       "aboutmeview": { 
       templateUrl: 'Home/Home' 
      } 
     } 
    }) 
    .state('Home', { 
     url: '/Home', 
     views: { 
      "aboutmeview": { 
       templateUrl:'/Home/Home' 
      } 
     } 
    }) 

    .state('About', { 
     url: '/About', 
     views: { 
      "aboutmeview": { 
       templateUrl: '/Home/About' 
      } 
     } 
    }) 
    .state('CV', { 
     url: '/CV', 
     views: { 
      "aboutmeview": { 
       templateUrl: '/Home/CV' 
      } 
     } 
    }) 
    .state('Projects', { 
     url: '/Projects', 
     views: { 
      "aboutmeview": { 
       templateUrl: '/Home/Projects' 
      } 
     } 
    }) 
    .state('Blog', { 
     url: '/Blog', 
     views: { 
      "aboutmeview": { 
       templateUrl: '/Home/Blog' 
      } 
     } 
    }) 
    .state('Contact', { 
     url: '/Contact', 
     views: { 
      "aboutmeview": { 
       templateUrl: '/Home/Contact' 
      } 
     } 
    }); 

} 
configFunction.$inject = ['$stateProvider', '$httpProvider', '$locationProvider']; 
aboutmeApp.config(configFunction); 

aboutmeApp.controller("ContactController", function ($scope,$http) { 

    $scope.alphabet = /^[A-z]+$/; 

    // $scope.emailaddress = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-][email protected][a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/; 

    $scope.submitForm = function() { 
debugger 
     // check to make sure the form is completely valid 
     $http({ 
      method: "POST", 
      headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, 
      url: "/Home/Sendmail" 


     }); 



    }; 

}); 

CSHTML文件

HTML

<--form name="myForm" ng-controller="ContactController" ng-class="{true:'error'}[submitted]" ng-submit="submitForm()"> 

     <p class=" username"> 
      <input type="text" name="user" ng-model="user" placeholder="NAME" required ng-pattern="alphabet" oninvalid="this.setCustomValidity('Please Enter UserName')" oninput="setCustomValidity('')" /> 
      <label for="name">Name</label> 
      <span style="color:red" ng-show="myForm.user.$dirty && myForm.user.$invalid"> 
       <span ng-show="myForm.user.$error.required">Username is required.</span> 
      </span> 
      <span style="color:red" class="error" ng-show="myForm.user.$error.pattern"> 
       Alphabets only 
      </span> 
     </p><br /> 

     <p class="mails"> 
      <input type="email" name="email" id="email" ng-model="email" placeholder="EMAIL" required oninvalid="this.setCustomValidity('Please Enter Email')" oninput="setCustomValidity('')" style="background-color:white" /> 
      <label for="mails">Email</label> 
      <span style="color:red" ng-show="myForm.email.$dirty && myForm.email.$invalid"> 
       <span ng-show="myForm.email.$error.required">Email is required.</span> 
       <span ng-show="myForm.email.$error.email">Invalid email address.</span> 
      </span> 
     </p><br /> 

     <p class="web"> 
      <input type="text" name="web" id="web" placeholder="SUBJECT" /> 
      <label for="web">Subject</label> 
     </p><br /> 

     <p class="text"> 
      <textarea name="text" placeholder="WRITE SOMETHING TO ME" /> 
     </p><br /> 

     <p class="submit"> 
      <input type="submit" value="Send" ng-click="submitted=true" /> 
     </p> 



    </form> 
+0

貴'submitForm'曾經執行?你對'$ http'的調用沒有指定任何數據,所以這就是爲什麼沒有數據發佈的原因。請參閱[文檔](https://docs.angularjs.org/api/ng/service/$http)。 – 2015-04-06 00:45:12

+0

$ http調用沒有視圖的問題。 Sendmail操作不具有視圖(html頁面)。這樣http post就不會調用這個動作。我需要註冊不帶tempelateUrl的發送郵件動作。但我不知道該怎麼做。請幫幫我。 – 2015-04-12 23:04:20

回答

1

您沒有任何數據傳遞到您的文章的行動,根本就使其通過爲了更好的代碼,我會創建一個模型對象來保存所有的v然後將其發佈到服務器端操作。

HTML

<form name="myForm" ng-controller="ContactController" ng-class="{true:'error'}[submitted]" ng-submit="submitForm(model)"> 
    <p class=" username"> 
     <input type="text" name="user" ng-model="model.user" placeholder="NAME" required ng-pattern="alphabet" oninvalid="this.setCustomValidity('Please Enter UserName')" oninput="setCustomValidity('')" /> 
     <label for="name">Name</label> 
     <span style="color:red" ng-show="myForm.user.$dirty && myForm.user.$invalid"> 
       <span ng-show="myForm.user.$error.required">Username is required.</span> 
     </span> 
     <span style="color:red" class="error" ng-show="myForm.user.$error.pattern"> 
       Alphabets only 
      </span> 
    </p> 
    <br /> 
    <p class="mails"> 
     <input type="email" name="email" id="email" ng-model="model.email" placeholder="EMAIL" required oninvalid="this.setCustomValidity('Please Enter Email')" oninput="setCustomValidity('')" style="background-color:white" /> 
     <label for="mails">Email</label> 
     <span style="color:red" ng-show="myForm.email.$dirty && myForm.email.$invalid"> 
       <span ng-show="myForm.email.$error.required">Email is required.</span> 
     <span ng-show="myForm.email.$error.email">Invalid email address.</span> 
     </span> 
    </p> 
    <br /> 

    <p class="web"> 
     <input type="text" name="model.web" id="web" placeholder="SUBJECT" /> 
     <label for="web">Subject</label> 
    </p> 
    <br /> 

    <p class="text"> 
     <textarea name="text" ng-model="model.comment" placeholder="WRITE SOMETHING TO ME" /> 
    </p> 
    <br /> 

    <p class="submit"> 
     <input type="submit" value="Send" ng-click="submitted=true" /> 
    </p> 

</form> 

代碼

aboutmeApp.controller("ContactController", function($scope, $http) { 
    $scope.alphabet = /^[A-z]+$/; 
    $scope.model = {}; 
    // $scope.emailaddress = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-][email protected][a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/; 
    $scope.submitForm = function(mdoel) { 
     // check to make sure the form is completely valid 
     $http({ 
      method: "POST", 
      headers: { 
       'Content-Type': 'application/x-www-form-urlencoded' 
      }, 
      url: "/Home/Sendmail", 
      data: { 
       mdoel: mdoel 
      } 
     }); 
    }; 
}); 
+0

它對你有幫助嗎? – 2015-07-19 14:15:40