如何实现ASP.NT MVC控制器?

时间:2019-05-23 05:36:01

标签: c# asp.net-mvc api

我是.NET FRAMEWORK的新手,并且正在尝试为房地产开发ASP.NET MVC API。 在我的数据库中,我有一个表Product and Amenity。我的假设是一个产品,例如公寓可以有多个设施,例如[{'Gym'},{'Wifi'}]。因此,我创建了另一个引用Product和Amenity的表ProductAmenity。

到目前为止,我已经提出了Product类,Amenity类和ProductAmenity类以及相应的ProductsController和ProductAmenitiesController。当前,当我在ProductsController上执行get请求时,它将返回所有其他值,除了在ObjectProductAmenity中返回NULL值之外,尽管它们是表ProductAmenity中的数据。

Here are my controllers:

namespace api_realEstate.Controllers
{
public class ProductsController : ApiController
    {
        readonly RealEstateDBEntities entities;
        public ProductsController()
        {
            entities = new RealEstateDBEntities();
        }
        private List<ProductView> PrepProducts(List<Product> dbProducts)
        {
            var productsView = new List<ProductView>();
            foreach (var product in dbProducts)
            {
                productsView.Add(
                    new ProductView
                    {
                        Id = product.ID,
                        Category = product.Category,
                        Address = product.Location,
                        Amount = product.Price,
                        Beds = product.Bathroom,
                        Baths = product.Bedroom,
                        Area = product.Area,
                        Parking = product.Parking,
                        ParkingSpot = product.ParkingSpot,
                        Description = product.Description,
                        Term = product.Term,
                        UserId = product.UserID
                    });
            }
            return productsView;
        }
        public IHttpActionResult Get()
        {
            var dbProducts = entities.Products.ToList();
            return Ok(PrepProducts(dbProducts));
        }
	}	
}	

namespace api_realEstate.Controllers
{
    public class AmenitiesController : ApiController
    {
        readonly RealEstateDBEntities entities;
        public AmenitiesController()
        {
            entities = new RealEstateDBEntities();
        }
        private List<AmenityView> PrepAmenities(List<Amenity> dbAmenities)
        {
            var amenitiesView = new List<AmenityView>();
            foreach (var amenity in dbAmenities)
            {
                amenitiesView.Add(
                    new AmenityView
                    {
                        Id = amenity.ID,
                        Itemname = amenity.ItemName                       
                    });
            }
            return amenitiesView;
        }
        public IHttpActionResult Get()
        {
            var dbAmenities = entities.Amenities.ToList();
            return Ok(PrepAmenities(dbAmenities));
        }
	}
}	

namespace api_realEstate.Controllers
{
    public class ProductAmenitiesController : ApiController
    {
        readonly RealEstateDBEntities entities;
        public ProductAmenitiesController()
        {
            entities = new RealEstateDBEntities();
        }
        private List<ProductAmenityView> PrepProductAmenities(List<ProductAmenity> dbProdAmenity)
        {
            var prodamenityView = new List<ProductAmenityView>();
            foreach (var prodamenity in dbProdAmenity)
            {
                prodamenityView.Add(
                    new ProductAmenityView
                    {
                        Id = prodamenity.ID,
                        Productid = prodamenity.ProductID,
                        Amenityid = prodamenity.AmenityID                        
                    });
            }
            return prodamenityView;
        }
        public IHttpActionResult Get()
        {
            var dbProdAmenity = entities.ProductAmenities.ToList();
            return Ok(PrepProductAmenities(dbProdAmenity));
        }
	}
}
Here are my classes:

public partial class Product
    {
        public Product()
        {
            this.ProductAmenities = new HashSet<ProductAmenity>();
            this.ProductImages = new HashSet<ProductImage>();
        }    
        public long ID { get; set; }
        public string Category { get; set; }
        public string Location { get; set; }
        public int Price { get; set; }
        public int Bedroom { get; set; }
        public int Bathroom { get; set; }
        public Nullable<int> Area { get; set; }
        public string Parking { get; set; }
        public Nullable<int> ParkingSpot { get; set; }
        public string Description { get; set; }
        public string Term { get; set; }
        public long UserID { get; set; }    
        public virtual User User { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<ProductAmenity> ProductAmenities { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<ProductImage> ProductImages { get; set; }
    }	
    
public partial class Amenity
    {      
        public Amenity()
        {
            this.ProductAmenities = new HashSet<ProductAmenity>();
        }    
        public long ID { get; set; }
        public string ItemName { get; set; }    
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<ProductAmenity> ProductAmenities { get; set; }
    }
    
public partial class ProductAmenity
    {
        public long ID { get; set; }
        public long ProductID { get; set; }
        public long AmenityID { get; set; }    
        public virtual Amenity Amenity { get; set; }
        public virtual Product Product { get; set; }
    }	

我希望当我对ProductsController进行(GET)请求时,我应该在ProductAmenity对象中返回值。 如何在我的控制器中应用这些表之间的关系:Product,Amenity和ProductAmenity?

2 个答案:

答案 0 :(得分:1)

我认为您需要这样包含:

 public IHttpActionResult Get()
 {
      var dbProducts = entities.Products.Include(x => x.ProductAmenities).ToList();
      return Ok(PrepProducts(dbProducts));
 }

然后在PrepProducts Action(而不是foreach)中使用以下命令:

  var productsView = dbProducts.Select(product => new ProductView {
                            Id = product.ID,
                            Category = product.Category,
                            Address = product.Location,
                            Amount = product.Price,
                            Beds = product.Bathroom,
                            Baths = product.Bedroom,
                            Area = product.Area,
                            Parking = product.Parking,
                            ParkingSpot = product.ParkingSpot,
                            Description = product.Description,
                            Term = product.Term,
                            UserId = product.UserID,
                            ProductAmenities = product.ProductAmenities.Select(//use viewmodel to cast it, like to ProductView )

                }).ToList();

答案 1 :(得分:-1)

In the productsView Model I just added this and it worked:

private List<ProductView> PrepProducts(List<Product> dbProducts)
        {
            var productsView = new List<ProductView>();
            foreach (var product in dbProducts)
            {
                productsView.Add(
                    new ProductView
                    {
                        Id = product.ID,
                        UserId = product.UserID,
                        Category = product.Category,
                        Address = product.Location,
                        Amount = product.Price,
                        Beds = product.Bathroom,
                        Baths = product.Bedroom,
                        Area = product.Area,
                        Parking = product.Parking,
                        ParkingSpot = product.ParkingSpot,
                        Description = product.Description,
                        Term = product.Term,

                        Amenities = product.ProductAmenities.Select(x => new Models.ProductAmenity
                        {
                            Id = x.ID,
                            Amenity = new AmenityView
                            {
                                Id = x.Amenity.ID,
                                Itemname = x.Amenity.ItemName
                            }
                        }).ToList()
                      
            });
            return productsView;
        }