2016-04-12 127 views
1

該系統用於管理電影租賃。用戶可以管理客戶,添加管理電影並管理租賃。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; 


     } 
+0

您的問題可能太寬泛。你能向我們展示任何嘗試嗎? Razor有一些使用下拉列表的非常直接的方法。如果你正在尋找一個教程,這可能不是正確的地方。 –

回答

1

創建特定這一觀點視圖模型。

public class RentMovieVm 
{ 
    public List<SelectListItem> Customers { set; get;} 
    public List<SelectListItem> Movies { set; get;} 

    public int CustomerId { set; get;} 
    public int MovieId { set; get;} 
} 

現在在你的GET行動,創建一個這樣的對象,從你的表填充CustomersMovies性能數據。

public ActionResult Rent() 
{ 
    var vm = new RentMovieVm(); 
    vm.Customers= dbContext.Customers.Select(s=> new SelectListItem { 
              Value =s.CustomerId.ToString(), 
              Text= s.FirstName+" "+ s.LastName }).ToList(); 
    vm.Movies= dbContext.Movies.Select(x=> new SelectListItem { 
               Value =x.MovieId.ToString(), 
               Text= x.Title }).ToList(); 
    return View(vm); 
} 

現在你的觀點會被強類型到這個視圖模型

@model RentMovieVm 
@using(Html.Beginform()) 
{ 
    <label> Select a customer </label> 
    @Html.DropDownListFor(s=>s.CustomerId, Model.Customers) 
    <label> Select a movie </label> 
    @Html.DropDownListFor(s=>s.MovieId, Model.Movies) 

    <input type="submit" /> 
} 

現在,在您HttpPost操作方法,您可以使用相同的RentMovieVm類作爲參數,以便模型粘結劑將能夠映射發佈的表單數據轉換爲此類的對象的屬性。

[HttpPost] 
public ActionResult Rent(RentMovieVm model) 
{ 
    var movieId = model.MovieId; 
    var customerId = model.CustomreId; 
    // to do : Save this to your Rental Table 
    // to do : Return something (redirect to success page) 
} 
+0

嗨,感謝您的評論。這隻在使用實體框架時才起作用嗎?我沒有使用實體框架。到目前爲止,我已經使用存儲過程來創建,編輯和刪除電影和客戶。 – JamesM94

+0

否。渲染下拉的基本概念是相同的。如果您未使用EF,請更新'Rent()'GET操作方法以從其他數據訪問方法獲取數據,而不是使用'dbContext.Customers'。例如:'yourDataAccessClass.GetCustomers()。Select(//現在的代碼)'其中'GetCustomers'返回'Customer'的集合。 – Shyju

+0

感謝。我仍然收到錯誤。我已經添加了上面的RentalsDataAccessClass和我的RentalsController。你能否看看並告訴我我哪裏可能會出錯?謝謝 – JamesM94