2015-07-10 65 views
1

我想收到FORMDATA中的ActionResult参数,我不想隐蔽到FORMDATA(),并使用的Request.Form在操作结果,我不能接收数据直接,无FORMDATA(),这里是我试过到目前为止...angularJS表单数据的ActionResult - ASP .NET MVC

<form ng-show="divPatient" name="PatientForm" ng-submit="AddUpdatePatient()"> 
      <table> 
       <tr> 
        <td> 
         <input class="form-control" type="text" readonly placeholder="Key (Automatic)" ng-model="model.PatientID" /> 
        </td> 
        <td> 
         <input name="FirstName" class="form-control" type="text" placeholder="FirstName" ng-model="model.FirstName" ng-minlength="3" required /> 
        </td> 
        <td> 
         <input name="LastName" class="form-control" type="text" placeholder="LastName" ng-model="model.LastName" ng-minlength="3" required /> 
        </td> 
        <td> 
         <input class="form-control" type="text" placeholder="Disease" ng-model="Disease" name="model.Disease" ng-minlength="3" required /> 
        </td> 
        <td> 
         <input class="form-control" type="number" placeholder="Phone No." ng-model="model.PhoneNo" name="PhoneNo" ng-pattern="/^[789]\d{9}$/" required /> 
        </td> 
        <td> 
         <input type="file" onchange="angular.element(this).scope().selectFileforUpload(this.files)" ng-model="model.PhotoURL" value="" class="form-control profilePic" required/> 
        </td> 
        <td colspan="2" class="saveCancel"> 
         <input type="submit" value="save" class="btn btn-success" ng-disabled="PatientForm.PhoneNo.$dirty && PatientForm.PhoneNo.$invalid || PatientForm.LastName.$dirty && PatientForm.LastName.$invalid || PatientForm.FirstName.$dirty && PatientForm.FirstName.$invalid || PatientForm.Disease.$dirty && PatientForm.Disease.$invalid" /> 
         <input type="button" value="cancel" class="btn btn-primary" ng-click="CancelForm()" /> 
        </td> 
       </tr> 
       <tr> 
        <td></td> 
        <td><span class="eror-text" ng-show="PatientForm.FirstName.$error.minlength">min 3 letters</span></td> 
        <td><span class="eror-text" ng-show="PatientForm.LastName.$error.minlength">min 3 letters</span></td> 
        <td><span class="eror-text" ng-show="PatientForm.Disease.$error.minlength">min 3 letters</span></td> 
        <td><span class="eror-text" ng-show="PatientForm.PhoneNo.$error.pattern">Invalid phone no</span></td> 
       </tr> 
      </table> 
     </form> 

我在angularJS控制器

app.controller("StudentCtrl", function ($scope, angularService) { 
$scope.model = {}; 
$scope.AddUpdatePatient = function() { 
    var getData = angularService.AddPatient($scope.model); 
    getData.then(function (msg) { 
      alert(msg.data); 
     }, function() { 
      alert('Error in adding record'); 
     }); 
    } 
} 

在angularJS我的服务

app.service("angularService", function ($http) { 

    // Add Employee 
    this.AddPatient = function (patientData) { 
     var response = $http({ 
      withCredentials: true, 
      headers: { 'Content-Type': undefined }, 
      transformRequest: angular.identity, 
      method: "post", 
      url: "AddPatient", 
      data: patientData, 
      dataType: "json" 
     }); 
     return response; 
    } 
} 

而且我的行为结果

public string AddPatient(Patient patientData) { 
    //but patientdata fields are all null 
} 

我的MVC模式

using ... 

namespace WebApplication3.Models 
{ 
    public class Patient 
    { 
     public int PatientID { get; set; } 
     public string FirstName { get; set; } 
     public string LastName { get; set; } 
     public long PhoneNo { get; set; } 
     public string Disease { get; set; } 
     public string PhotoURL { get; set; } 
    } 
} 

我不知道为什么在行动结果patientData为空?

+0

的默认HTTP标头的内容类型是application/JSON进行angularjs如上述[这里](https://docs.angularjs.org/api/ng/service/$http),将其更改为'application/x-www-form-urlencoded'。也看到这个【答案】(http://stackoverflow.com/a/11443066/154219) – Adeel

回答

2

尝试此

$http({ method: 'POST', url: '/ControllerName/AddPatient', data: patientData }). 
          success(function (data, status, headers, config) { 

          }). 
          error(function (data, status, headers, config) { 
           alert(status); 
          }); 
+0

使用完整的URL,我认为。 –

+0

没有这没有帮助我:( – Arjun