ASP.NET CORE MVC 응용 프로그램에서 매우 기본적인 작업을했습니다. 그래서 것은 내가 2 모델 구독 및 쿠폰 을 가지고 있고 그들은 많은 관계에 1 대가 있고 다음 모델 클래스와 스캐 폴딩을 사용하여 쿠폰에 dropDownList 를 생성하여 현재 데이터베이스에있는 구독 중 하나를 선택하는 페이지를 만들 수있었습니다. 내 데이터베이스와 스 캐 폴딩을위한 Entity Framework도 사용하고 있습니다.
모델 클래스
public class Subscription
{
public string FirstName { get; set; }
[Key]
public int ID { get; set; }
[Required]
public string Identifier { get; set; }
public bool State { get; set; }
[Required]
public string Description { get; set; }
[Required]
public int MonthlyPrice { get; set; }
[Required]
public int MinMonthlyPrice { get; set; }
[Required]
public int MonthlyDiscount { get; set; }
public virtual ICollection<Coupon> Coupons { get; set; }
public virtual ICollection<RegisterViewModel> Registrations { get; set; }
}
public class Coupon
{
[Required]
public string CouponCode { get; set; }
[Required]
public DateTime StartTime { get; set; }
[Required]
public DateTime EndTime { get; set; }
[Key]
public int ID { get; set; }
public int Discount { get; set; }
public bool IsPercentage { get; set; }
[ForeignKey("Identifier")]
public int SubscriptionId { get; set; }
public virtual Subscription Identifier { get; set; }
[Required]
public int ValidMonths { get; set; }
public bool IsSponsored { get; set; }
[Required]
public int Length { get; set; }
[Required]
public int Quantity { get; set; }
}
이제 등록 페이지에서 동일한 드롭 다운을 원합니다. 개별 사용자 계정 의 내장 된 등록 시스템을 사용하고 있습니다. 동일한 드롭 다운을 포함시키고 싶습니다. "가입"을 선택하기 위해 쿠폰 페이지에 있습니다. 동일한 것을 원합니다. 등록 페이지. 내가 어떻게 할 수 있니? AccountsController 는 "couponsController"와 같은 다른 컨트롤러와 달리 생성자 매개 변수에 ApplicationDbContext 를 가져 오지 않으므로 등록 페이지에서 데이터베이스에서 데이터를 가져 오는 방법을 찾는 데 문제가 있습니다.
다음 코드는 구독 목록을 가져 와서 특정 속성 만 가져 오는 데 도움이된다는 것을 알고 있습니다.
// GET: Coupons/Create
public IActionResult Create()
{
ViewData["SubscriptionId"] = new SelectList(_context.Subscription, "ID", "Identifier");
return View();
}
내 등록 페이지에 같은 종류의 물건을 원하지만 dbcontext가 AccountsController에 존재하지 않고 bcz를 새로 만들 수 없다. 생성자에서 제공 할 옵션이 무엇인지 모르겠다.
이미 DbContext를 가지고 있다면 다른 컨트롤러와 비슷한 생성자를 생성하면 자동으로 삽입되어야합니다.
private ApplicationDbContext _db;
public AccountsController(ApplicationDbContext db)
{
_db = db;
}
그런 다음이를 통해 DbSets에 액세스 할 수 있습니다. 그게 작동하지 않는다면 Startup 클래스의 ConfigureServices에서 비슷한 것이 있는지 확인하십시오 :
services.AddTransient<ApplicationDbContext>();
다른 컨트롤러가 DbContext에 대한 액세스 권한을 갖고 있으면 해당 행과 비슷한 항목이 이미 있어야합니다.