I have a very basic task in my ASP.NET CORE MVC application. so the thing is I have 2 models Subscriptions and Coupons and they have one to many relations and by using following model classes and scaffolding I was able to generate a dropDownList on coupons create page to select anyone of the subscriptions currently in the database. I am using Entity Framework for my db and scaffolding as well.
Model classes
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; }
}
now I want the same drop down on my Registration page, I am using the built in registration system of Individual User accounts and I just want to include the same drop down I have on coupon page to select "subscription" I want the same one on registration page. how can I do that? AccountsController is not getting any ApplicationDbContext in its constructor parameter unlike other controllers like "couponsController" so that is why I am having trouble figuring out how to get data from database on registration page.
I know that following code helps to get me the subscription list and get only a specific property from it.
// GET: Coupons/Create
public IActionResult Create()
{
ViewData["SubscriptionId"] = new SelectList(_context.Subscription, "ID", "Identifier");
return View();
}
I want same sort of stuff on my registration page but dbcontext doesn't exist on AccountsController and I cannot new it up bcz I don't know what options to provide in the constructor.
If you already have your DbContext, it should just automatically be injected if you create a constructor similar to your other controllers.
private ApplicationDbContext _db;
public AccountsController(ApplicationDbContext db)
{
_db = db;
}
Then you can access your DbSets through that. If that’s not working then check if you have something similar in ConfigureServices in your Startup class:
services.AddTransient<ApplicationDbContext>();
If your other controllers have access to the DbContext something similar to that line should already be there.