I am new to asp.net mvc. I am making an online video store application.
I have this view:
where users can choose which videos their gonna rent. The code for the view:
@model IEnumerable<VideoRentalSystem.Models.VideoMaintenance>
@{
ViewBag.Title = "Index";
}
<h2>Video Rentals</h2>
<h5 style="color:red">Only available videos is shown.</h5>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Title)
</th>
<th>
@Html.DisplayNameFor(model => model.Description)
</th>
<th>
@Html.DisplayNameFor(model => model.Genre)
</th>
<th>
@Html.DisplayNameFor(model => model.Rating)
</th>
<th>
@Html.DisplayNameFor(model => model.Quantity)
</th>
<th>
@Html.DisplayNameFor(model => model.UnitsAvailable)
</th>
<th>
@Html.DisplayNameFor(model => model.RentalPrice)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.Description)
</td>
<td>
@Html.DisplayFor(modelItem => item.Genre)
</td>
<td>
@Html.DisplayFor(modelItem => item.Rating)
</td>
<td>
@Html.DisplayFor(modelItem => item.Quantity)
</td>
<td>
@Html.DisplayFor(modelItem => item.UnitsAvailable)
</td>
<td>
@Html.DisplayFor(modelItem => item.RentalPrice)
</td>
<td>
@Html.CheckBox("Rent?", new { id= item.VideoMaintenanceID})
</td>
</tr>
}
</table>
<input class="btn btn-primary" type="submit" value="Rent Selected Videos" />
Like you can see at the bottom of the code where I add the checkbox I (hopefully) give the checkbox an ID equal to the video info left of the textbox. What I want to know is how will I, if the user clicks the 'Rent Selected Videos' button, get all the selected videos and add it to a list, inside the controller-hope this makes sense?
My current controller:
using System.Data;
using System.Linq;
using System.Web.Mvc;
using VideoRentalSystem.DAL;
namespace VideoRentalSystem.Controllers
{
public class VideoRentalsController : Controller
{
private VideoRentalContext db = new VideoRentalContext();
// GET: VideoRentals
public ActionResult Index()
{
var available = from a in db.VideosMaintenance
select a;
available = available.Where(av => av.UnitsAvailable != 0);
return View(available);
}
}
}
My Model for the VideoMaintenance:
//Primary Key
public int VideoMaintenanceID { get; set; }
[StringLength(60, MinimumLength = 3, ErrorMessage = "A maximum of 60 and a minimum of 3 characters is allowed")]
[Required(ErrorMessage = "This field is required")]
public string Title { get; set; }
[RegularExpression(@"^[A-Z]+[a-zA-Z''-'\s]*$", ErrorMessage = "Only letters is allowed")]
[Required(ErrorMessage = "This field is required")]
[StringLength(140, ErrorMessage = "A maximum of 140 characters is allowed")]
public string Description { get; set; }
[RegularExpression(@"^[A-Z]+[a-zA-Z''-'\s]*$", ErrorMessage = "Only letters is allowed")]
[Required(ErrorMessage = "This field is required")]
[StringLength(30, ErrorMessage = "A maximum of 30 characters is allowed")]
public string Genre { get; set; }
[StringLength(15, ErrorMessage = "A maximum of 15 characters is allowed")]
public string Rating { get; set; }
[RegularExpression(@"^[0-9]+$", ErrorMessage = "Only numbers is allowed")]
[Required(ErrorMessage = "This field is required")]
public int Quantity { get; set; }
[Display(Name = "Units Available")]
[RegularExpression(@"^[0-9]+$", ErrorMessage = "Only numbers is allowed")]
[Required(ErrorMessage = "This field is required")]
public int UnitsAvailable { get; set; }
[Display(Name = "Rental Price")]
[Range(1, 100, ErrorMessage = "Range between 1,100")]
[DataType(DataType.Currency)]
public decimal RentalPrice { get; set; }
//Foreign Key
public virtual ICollection<CustomerMaintenance> CustomersMaintenance { get; set; }
}
...and my model for CustomerMaintenance:
//Primary Key
[Display(Name = "Identification Number")]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
[Required(ErrorMessage = "This field is required")]
[RegularExpression(@"^[0-9]+$", ErrorMessage = "Only numbers is allowed")]
public string CustomerMaintenanceID { get; set; }
[StringLength(30, ErrorMessage = "A maximum of 30 characters is allowed")]
[RegularExpression(@"^[A-Z]+[a-zA-Z''-'\s]*$", ErrorMessage = "Only letters is allowed")]
[Required(ErrorMessage = "This field is required")]
public string Name { get; set; }
[StringLength(60, ErrorMessage = "A maximum of 60 characters is allowed")]
[RegularExpression(@"^[A-Z]+[a-zA-Z''-'\s]*$", ErrorMessage = "Only letters it allowed")]
[Required(ErrorMessage = "This field is required")]
public string Surname { get; set; }
[StringLength(250, ErrorMessage = "A maximum of 250 characters is allowed")]
//[RegularExpression(@"^[A-Z]+[0-9]+[a-zA-Z''-'\s]*$")]
[Required(ErrorMessage = "This field is required")]
[Display(Name = "Physical Address")]
public string Address { get; set; }
[StringLength(10, ErrorMessage = "A maximum of 10 characters is allowed")]
[RegularExpression(@"^[0-9]+$", ErrorMessage = "Only numbers allowed")]
[Required(ErrorMessage = "This field is required")]
[Display(Name = "Phone Number")]
public string PhoneNumber { get; set; }
//Foreign Key
public virtual VideoMaintenance VideosMaintenance { get; set; }
My DbContext:
using System.Data.Entity;
using VideoRentalSystem.Models;
using System.Data.Entity.ModelConfiguration.Conventions;
namespace VideoRentalSystem.DAL
{
public class VideoRentalContext : DbContext
{
//Passes name of the connnection string to the constructor
public VideoRentalContext() : base("VideoRentalContext")
{
}
public DbSet<CustomerMaintenance> CustomersMaintenance { get; set; }
public DbSet<VideoMaintenance> VideosMaintenance { get; set; }
//Override entity naming convention- disable pluralisation
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingEntitySetNameConvention>();
}
}
}
I apologize for this long question! Any help will be appreciated!