2014-12-03 23 views
0

我是新角度。我正在创建一个实践项目。我正在关注This tutorial。 一切工作正常,除非当我尝试把数据注释放在我的视图模型$ resource.save方法不再有效。没有数据注释,它工作正常。

这里是我的代码:

的Home.html:

<!DOCTYPE html> 
<html ng-app="movieModule"> 
<head> 
    <meta name="viewport" charset="UTF-8" content="width=device-width, initial-scale=1"> 
    <title>Movie Sample</title> 


    <link href="../Content/bootstrap.min.css" rel="stylesheet" /> 
    <script src="../Scripts/angular.min.js"></script> 
    <script src="../Scripts/angular-resource.min.js"></script> 
    <script src="../Scripts/angular-route.min.js"></script> 
    <script src="../Scripts/jquery-2.1.1.min.js"></script> 
    <script src="../Scripts/bootstrap.min.js"></script> 
    <script src="../Scripts/movie-module.js"></script> 
    <script src="../Scripts/Category/category-controller.js"></script> 
    <script type="text/javascript"> 
     $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) { 
      var target = $(e.target).attr("href") // activated tab 
     }); 
    </script> 
</head> 

<body> 
    <div class="container"> 
     <div class="container-fluid"> 
      <div class="well"> 
       <ul class="nav nav-pills"> 
        <li role="presentation" class="active"><a href="home.html" data-toggle="tab">Home</a></li> 
        <li role="presentation"><a href="#" data-toggle="tab">Movies</a></li> 
        <li role="presentation"><a href="categories.html" data-toggle="tab">Categories</a></li> 
        <li role="presentation"><a href="#" data-toggle="tab">Artists</a></li> 
       </ul> 
      </div> 
     </div> 
     <div class="container-fluid"> 
      <div ng-view=""> 
      </div> 
     </div> 
    </div> 
</body> 
</html> 

Categories.html:

<div> 
    <a href="create-category.html" class="btn btn-primary"> 
     <span class="glyphicon glyphicon-plus" aria-hidden="true"></span> 
    </a> 
</div> 

<div class="panel panel-default"> 
    <!-- Default panel contents --> 
    <div class="panel-heading">Categories</div> 

    <!-- Table --> 
    <table class="table"> 
     <tr> 
      <th>ID 
      </th> 
      <th>Name 
      </th> 
      <th>Description 
      </th> 
     </tr> 
     <tr data-ng-repeat="category in categories"> 
      <td>{{ category.id }} 
      </td> 
      <td>{{ category.name }} 
      </td> 
      <td>{{ category.description }} 
      </td> 
     </tr> 
    </table> 
</div> 

创建-category.html

<div class="row"> 
    <div class="col-md-12"> 
     <h3>Create Category 
     </h3> 
    </div> 
</div> 
<!--<div class="row">--> 

<div class="col-md-12"> 
    <div class="form-group"> 
     <label for="id">Id:</label> 
     <input type="text" ng-model="category.Id" class="form-control"> 
    </div> 
    <div class="form-group"> 
     <label for="name">Name:</label> 
     <input type="text" ng-model="category.Name" class="form-control"> 
    </div> 
    <div class="form-group"> 
     <label for="description">Description:</label> 
     <input type="text" ng-model="category.Description" class="form-control"> 
    </div> 
    <button type="submit" class="btn btn-primary" ng-click="save(category)">Create Category</button> 
</div> 

movieModule.js

var movieModule = angular.module('movieModule', ['ngRoute', 'ngResource']); 

movieModule.config(function ($routeProvider, $locationProvider) { 
    $routeProvider.when('/templates/categories.html', { templateUrl: '/templates/categories.html', controller: 'categoryController' }). 
        when('/templates/create-category.html', { templateUrl: '/templates/create-category.html', controller: 'categoryController' }); 
    $locationProvider.html5Mode(true); 
}); 

类别-controller.js

movieModule.controller('categoryController', function ($scope, $resource, $location) { 
    $scope.categories = $resource('/api/Category').query(); 

    $scope.save = function (category) { 
     $scope.errors = []; 
     $resource('/api/Category').save(category).$promise.then(
      function() { $location.url('templates/categories.html'); }, 
      function (response) { 
       $scope.errors = response.data; 
      }); 
    } 
}); 

CategoryVM.cs

public class CategoryVM 
{ 
    [Required(ErrorMessage="Is is required.")] 
    public int Id { get; set; } 

    public string Name { get; set; } 

    public string Description { get; set; } 
} 

问题是[Required(ErrorMessage="Is is required.")]。一切工作正常,而不[Required(ErrorMessage="Is is required.")]但现在我把它[Required(ErrorMessage="Is is required.")]开始给错误: 以下是错误的快照:

Snapshot of the error

+0

与注释,'Id'变为强制性的。您是否在请求中传递了ID? – Mrchief 2014-12-03 20:58:37

+0

不,我提供Id。但它仍然没有与CategoryVM进行映射。 – 2014-12-04 03:44:27

回答

1

尽量不要把[必需]属性上的ID。 如果您将数据注释放在其他属性上,它应该可以工作。

[必需]对值型(INT)将导致一个500错误 (字符串不是值类型)

+0

哇。多谢老兄。我在头撞了3天。你能告诉我如何验证值类型。 – 2014-12-04 03:55:42