Below is my Angular js Front end Code
app.controller("assetController", ['$scope', '$http', function ($scope, $http) {
$scope.submit = function () {
var book = {
"Name": $scope.name,
"Isbn": $scope.isbn,
"Publisher": $scope.publisher,
"Edition":$scope.edition
}
$http.post("http://localhost:51227/api/Asset", book)
.then(function(data, status) {
$scope.name = book.Name;
$scope.isbn = book.Isbn;
$scope.publisher = book.Publisher;
$scope.edition=book.Edition;
alert("product added successfully. " + status);
})
.catch(function(response) {
console.log('Exception Caught', response.status, response.data);
})
.finally(function() {
});
}
This is my webApi Controller
public void Post([FromBody] AssetViewModel model)
{
if ( model is BookViewModel)
{
BookViewModel bookViewModel = (BookViewModel)model;
AssetViewModel assetViewModel = new BookViewModel
{
Name = bookViewModel.Name,
Category = bookViewModel.Category,
Edition = bookViewModel.Edition,
Isbn = bookViewModel.Isbn,
Publisher = bookViewModel.Publisher
};
_assetAssetRepository.SaveAsset(Mapper.Map<Book>(assetViewModel));
}
}
Below are the Two ViewModel Classes
public class AssetViewModel
{
public string Name { get; set; }
}
public class BookViewModel:AssetViewModel
{
public string Isbn { get; set; }
public string Edition { get; set; }
public string Publisher { get; set; }
public string Category { get; set; }
}
So basically this is a simple Angular js Code that makes a call to WebAPI Controller and passes Book Object ( or the BookViewModel). The Parameter for the WebApi is AssetViewModel (BaseClass) but its ok because we should be able to pass in BookViewModel(Derived Class) at runtime. The Angular Front End passes Book Object but when i check in the webapi controller it shows up as a parent class i.e. Asset. Therefore i cannot retrive the fields associated with Book Class. Could someone please show me the right way?