該系統用於管理電影租賃。用戶可以管理客戶,添加管理電影並管理租賃。ASP.net MVC - 使用下拉列表
這裏是我的模型:
客戶提供樣板
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; }
}
}
電影型號:
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; }
}
}
出租型號:
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; }
}
}
在我的公寓查看我想顯示一個下拉菜單包含所有客戶名字+姓氏的列表。我也想顯示一個包含所有電影標題的下拉列表。
當用戶從下拉列表中選擇一個客戶和電影並單擊保存時,我希望所選客戶和所選電影的相關ID存儲在出租表中。
我該如何去做這件事?
編輯
這是我RentalsController內的AddRental代碼:
// 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();
}
}
這裏是我的RentalsDataAccess類中的代碼AddRental:
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;
}
您的問題可能太寬泛。你能向我們展示任何嘗試嗎? Razor有一些使用下拉列表的非常直接的方法。如果你正在尋找一個教程,這可能不是正確的地方。 –