OverstockEntities.cs
namespace Overstock.Models
{
public class OverstockEntities: DbContext
{
public DbSet<Product> Products { get; set; }
public DbSet<Category> Categories { get; set; }
}
}
Product.cs
namespace Overstock.Models
{
[Bind(Exclude = "ProductId")]
public class Product
{
[ScaffoldColumn(false)]
public int ProductId { get; set; }
[DisplayName("Category")]
public int CategoryId { get; set; }
[DisplayName("Brand")]
public int BrandId { get; set; }
[Required(ErrorMessage="Product title is required")]
[StringLength(160)]
public string Title { get; set; }
[Required(ErrorMessage="Price is required")]
[Range(0.01, 100000.00,
ErrorMessage="Price must be between 0.01 and 100000.00")]
public decimal Price { get; set; }
[DisplayName("Product Art URL")]
[StringLength(1024)]
public string PictureUrl { get; set; }
[Required(ErrorMessage = "Description is required")]
[StringLength(1024)]
public string Description { get; set; }
public virtual Category Category { get; set; }
public virtual Brand Brand { get; set; }
public virtual List<OrderDetail> OrderDetails { get; set; }
}
}
Category.cs
namespace Overstock.Models
{
public class Category
{
public int CategoryId { get; set; }
public string Name { get; set; }
public List<Product> Products { get; set; }
}
}
浏览动作
public ActionResult Browse(string category, string sortOrder)
{
var varCategories = MyDB.Categories.Include("Products").Single(c => c.Name == category);
//How to order by Product's Title value
return View(varCategories);
}
问题:如何获取另一个模型的变量(在本例中为Product.Title)以便按该变量排序?
答案 0 :(得分:0)
foreach (var category in varCategories )
{
category.Products = category.Products.OrderBy(p => p.Title);
}