【问题标题】:Angularjs Model passing null to MVC controllerAngularjs模型将null传递给MVC控制器
【发布时间】:2017-05-08 20:29:19
【问题描述】:

以下代码从 Angularjs 控制器向 MVC 控制器发送空对象。在“batarang”$scope 中显示 EmployeeInfo 对象,并用适当的值填写 HTML 表单。但是 MVC 方法使所有值都为空。我的代码如下

控制器:

Angular.module("myApp", []).controller("EmployeeBasicCtrl", function ($scope, $http) {
$scope.signBox = false;

$scope.SEX = [
    { text: "Non of above", value: "N" },
    { text: "Female", value: "F" },
    { text: "Male", value: "M" },

]
$scope.submitBasicInfo = function () {

    $http({
        method: 'POST',
        url: '/EmployeeInfo/AddEmployee'
    }).success(
    function (resp) {
        $scope.success = resp.success;
        $scope.Message = resp.Message;
    }
)// success en

    } // end of submit form
})// end of controller

HTML:

    <form ng-submit="submitBasicInfo()" name="EmployeeBasic" ng-controller="EmployeeBasicCtrl">

    <div class="well">
        <input type="hidden" name="EmpID" />
        <div class="panel panel-heading panel-primary">
            <div class="panel-heading"><h3> Personal Information </h3></div>
            <label> First Name </label>
            <input name="Fname" class="form-control" ng-model="EmployeeInfo.Fname" required />
            <small ng-show="EmployeeBasic.Fname.$touched && EmployeeBasicEmployeeBasic.Fname.$invalid">First Name is mandatory</small><br />
..........

MVC 控制器:

[HttpPost]
    public JsonResult AddEmployee(EmployeeInfo para)
    {
        return Json(new {success="success" },JsonRequestBehavior.AllowGet);
    }

EmployeeInfo 类:

    public class EmployeeInfo
{
    public string EmpID { get; set; }
    public string Fname { get; set; }
    public string Sname { get; set; }
    public DateTime DOB { get; set; }
    public string Email { get; set; }
    public string Mobile { get; set; }
    public string Gender { get; set; }
    public string UnitNo { get; set; }
    public string StreetNo { get; set; }
    public string Suburb { get; set; }
    public string StateID { get; set; }
    public string PostCode { get; set; }

感谢您的帮助

【问题讨论】:

    标签: angularjs model-view-controller model null


    【解决方案1】:

    这是因为您没有通过服务发布数据,这就是您在后端得到 null 的原因

    你的发帖请求应该是这样的

     $scope.EmployeeInfo ={'Fname':'',EmpID:''....all your child obj of EmployeeInfo }
    
    $scope.submitBasicInfo = function (EmployeeInfo) {
    
        $http({
            method: 'POST',
            url: '/EmployeeInfo/AddEmployee',
            data: EmployeeInfo, // what data you want to send 
            headers: {'Content-Type': 'application/json'}
        });
    }
    

    您可以从视图中传递 EmployeeInfo 对象

      <form ng-submit="submitBasicInfo(EmployeeInfo)" name="EmployeeBasic" ng-controller="EmployeeBasicCtrl">
    

    【讨论】:

    • 添加建议行后,我收到 ReferenceError: EmployeeInfo is not defined 错误
    • 亲爱的 Shailendra,感谢您的帮助,通过在 $http 函数中添加数据:$scope.Employeeinfo 解决了这个问题。我的坏
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-23
    • 2016-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多