"电影"模特儿"名字"内部对象的属性"演员"

时间:2017-06-02 08:14:45

标签: angularjs asp.net-mvc entity-framework asp.net-web-api asp.net-web-api2

我是Angular和Entity框架的新手。 这是我的AngularJS控制器的代码:

    $scope.add = function () {
    $scope.loading = true;
    alert(this.newMovie.Name);
    debugger;
    $http.post('api/Movie/', this.newMovie).then(function onSuccess(response) {
        alert("Added Successfully!!");
        debugger;
        $scope.showAddMovieForm = false;
        $scope.movies.push(response);
        $scope.loading = false;
    }).catch(function (response) {
        $scope.error = "An Error has occured while adding movie! :(" + response.data;
        $scope.loading = false;
    });

};

以下是Action方法在MVC中的MovieController中的外观:

public HttpResponseMessage Post(Movie movie)
{
    if (ModelState.IsValid)
    {
       // _db.People.Where(na => movie.Actors.Any(a => a.PersonId == na.PersonId));
        _db.Movies.Add(movie);
        _db.SaveChanges();
        HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, movie);
        response.Headers.Location = new Uri(Url.Link("DefaultApi", new { movieId = movie.MovieId }));
        return response;
    }
    else
    {
        return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
    }
}

电影模特课程:

public partial class Movie
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Movie()
    {
        this.Actors = new HashSet<Person>();
    }

    public int MovieId { get; set; }
    public string Name { get; set; }
    public Nullable<short> YearOfRelease { get; set; }
    public string Plot { get; set; }
    public byte[] Poster { get; set; }
    public Nullable<int> ProducerId { get; set; }

    public virtual Person Producer { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Person> Actors { get; set; }
}

当我在JS代码中放置调试器时,我可以看到Angular正在以正确的格式传递对象。

但不知何故,当它到达Action时,&#34;电影&#34;模特需要&#34;姓名&#34;财产价值&#34;演员&#34;对象是&#34;电影&#34;本身。

Debugger Screeshot:

ActionMethod, Movie Model Screenshot

无法理解为什么&#34;电影&#34;模特正在获得&#34;名称&#34; &#34;演员&#34;。

的财产

1 个答案:

答案 0 :(得分:1)

您需要修改在Movie对象中创建Actors对象的方式。

Actor应该是JS对象数组,调试时您的Movie对象应如下所示:

enter image description here

但是,在你的情况下,Actors只是一个对象。希望它有所帮助。