该系统用于管理电影租赁。用户可以管理客户,添加管理电影和管理租赁。
以下是我的模特:
客户模型
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 :(得分:1)
创建一个特定于此视图的视图模型。
<table border="1" data-ng-app="testModule" data-ng-controller="testController">
<thead>
<tr>
<th ng-repeat="i in headerYears">{{i}}</th>
</tr>
</thead>
<tbody>
<tr >
<td ng-repeat="i in headerYears" >{{i}}
<span ng-repeat="row in rows"></span>
<span ng-repeat="item in row.Col">{{item.Y}} </span>
</td>
</tr>
</tbody>
</table>
现在,在您的GET操作中,创建一个此对象,使用表格中的数据填充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;}
}
和Customers
属性。
Movies
现在,您的视图将强烈输入此视图模型
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);
}
现在,在您的HttpPost操作方法中,您可以使用相同的@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" />
}
类作为参数,因此模型绑定器将能够将发布的表单数据映射到此类对象的属性。
RentMovieVm