The system is for managing movie rentals. A user can Manage Customers, Add Manage Movies, and manage Rentals.
Here are my models:
Customers Model
namespace u1265196_MovieRentals.Models
{
public class Customers
{
[Display(Name = "Customer ID")]
[Key]
public int CustomerID { get; set; }
[Required]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Required]
[Display(Name = "Last Name")]
public string LastName { get; set; }
[Required]
[Display(Name = "Address Line 1")]
public string AddressLine1 { get; set; }
[Required]
[Display(Name = "Address Line 2")]
public string AddressLine2 { get; set; }
[Required]
[Display(Name = "City")]
public string City { get; set; }
[Required]
[Display(Name = "Postcode")]
public string Postcode { get; set; }
[Required]
[Display(Name = "Phone No")]
public string Phone { get; set; }
public virtual ICollection<Rentals> Rentals { get; set; }
}
}
Movies Model:
namespace u1265196_MovieRentals.Models
{
public class Movies
{
public int MovieID { get; set; }
[Required]
public string Title { get; set; }
[Required]
public string Director { get; set; }
[Required]
public string Genre { get; set; }
[Required]
[Display(Name = "Length of film")]
public string Length { get; set; }
[Required]
public int AgeRating { get; set; }
public virtual ICollection<Rentals> Rentals { get; set; }
}
}
Rentals Model:
namespace u1265196_MovieRentals.Models
{
public class Rentals
{
[Key]
public int RentalID { get; set; }
public int CustomerID { get; set; }
public int MovieID { get; set; }
public System.DateTime DateRented { get; set; }
public System.DateTime ReturnDate { get; set; }
[ForeignKey("CustomerID")]
public virtual Customers Customers { get; set; }
[ForeignKey("MovieID")]
public virtual Movies Movies { get; set; }
}
}
In my Rentals View i want to display a dropdown list containing all Customers First + Last Names. i also want to display a dropdown list containing all Movie Titles.
When a user selects a customer and movie from the dropdown lists and clicks save, i want the relevant ID's of the selected customer and selected movie to be stored in the Rentals Table.
How do i go about doing this?
EDIT
Here is the code for AddRental within my RentalsController:
// GET: Rentals/Create
public ActionResult AddRental()
{
var vm = new RentMovieVm();
vm.Customers = CustomerDataAccess.GetAllCustomers().Select(s => new SelectListItem { Value = s.Id.ToString(), Text = s.FirstName + " " + s.LastName }).ToList();
vm.Movies = MoviesDataAccess.GetAllMovies().Select(x => new SelectListItem { Value = x.Id.ToString(), Text = x.Title }).ToList();
return View(vm);
}
// POST: Rentals/Create
[HttpPost]
public ActionResult AddRental(RentMovieVm model)
{
var movieID = model.MovieID;
var customerID = model.CustomerID;
try
{
if (ModelState.IsValid)
{
RentalsDataAccess RentalsDA = new RentalsDataAccess();
if (RentalsDA.AddRental(model))
{
ViewBag.Message = "Rental added successfully";
}
}
return View();
}
catch
{
return View();
}
}
Here is the code AddRental within my RentalsDataAccess Class:
public bool AddRental(RentMovieVm obj)
{
connection();
SqlCommand com = new SqlCommand("AddNewRental", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@CustomerID", obj.CustomerID);
com.Parameters.AddWithValue("@MovieID", obj.MovieID);
con.Open();
int i = com.ExecuteNonQuery();
con.Close();
if (i >= 1)
{
return true;
}
else
{
return false;
}
}
Edit2
CustomersDataAccess:
public List<Customers> GetAllCustomers()
{
connection();
List<Customers> CustomerList = new List<Customers>();
SqlCommand com = new SqlCommand("GetCustomers", con);
com.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(com);
DataTable dt = new DataTable();
con.Open();
da.Fill(dt);
con.Close();
foreach (DataRow dr in dt.Rows)
{
CustomerList.Add(
new Customers
{
CustomerID = Convert.ToInt32(dr["CustomerID"]),
FirstName = Convert.ToString(dr["FirstName"]),
LastName = Convert.ToString(dr["LastName"]),
}
);
}
return CustomerList;
}