选中哪个复选框(控制器)和结果列表

时间:2015-11-11 20:23:37

标签: asp.net-mvc

我是asp.net mvc的新手。我正在制作一个在线视频商店应用程序。

我有这个观点:

enter image description here

用户可以选择要租用的视频。视图的代码:

@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" />

就像你可以在代码底部看到我添加复选框的地方我(希望)给复选框一个ID等于文本框左边的视频信息。我想知道的是,如果用户点击“租借所选视频”,我该怎么做?按钮,获取所有选定的视频并将其添加到控制器内的列表中 - 希望这有意义吗?

我现在的控制人员:

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);
        }
    }
}

我的视频维护模型:

//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; }
    }

...和我的客户维护模型:

//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; }

我的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>();
        }
    }
}

我为这个长期问题道歉!任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:3)

看起来是一个很好的用例,可以使用编辑器模板

为您的视图创建这样的视图模型。

public class RentVideoVM
{       
    public List<VideoSelection> Videos { set; get; } 
    //Add other properties needed for your view.
}

public class VideoSelection
{
    public int VideoId { set; get; }
    public string Name { set; get; }   

    public bool IsSelected { set; get; }
   //Add other properties needed for your view.
}

IsSelected类的VideoSelection属性用于在UI的复选框中存储用户选择。

现在,在您的GET操作方法中,创建RentVideoVM视图模型的实例,加载视频集合并将其发送到视图,

public ActionResult Register()
{
    var vm = new RentVideoVM();
    vm.Videos = db.Videos.Select(s => new VideoSelection
    {
        VideoId = s.VideoId,
        Name = s.VideoName
    }).ToList();
    return View(vm);
}

更改db.YourWhateverDBSet而不是db.Videos

现在我们需要创建一个编辑器模板。转到Views文件夹并进入当前控制器的文件夹,并创建一个名为“EditorTemplates”的子文件夹。在那里添加一个名为VideoSelection.cshtml的新视图,并在那里添加以下代码。

@model YourNamespace.VideoSelection
<div>
    @Model.Name

    @Html.CheckBoxFor(d=>d.IsSelected)

    @Html.HiddenFor(s => s.VideoId)
</div>

您可以根据需要更改html标记。让它看起来很棒!

enter image description here

现在,在您的主视图中,绑定到我们的RentVideoVM视图模型,调用Html.EditorFor辅助方法以使用复选框呈现视频。

@model YourNamespace.RentVideoVM
@using (Html.BeginForm())
{
    @Html.EditorFor(s=>s.Videos)
    <input type="submit"/>
}

您处理表单发布的HttpPost操作将是

[HttpPost]
public ActionResult Register(RentVideoVM model)
{
   //check model.Videos collection
   // to do : Save and redirect
   // If something wrong happened, reload Videos collection 
    return View(model);
}

当用户发布表单时,您可以检查已发布模型的Videos集合。 IsSelected属性适用于用户检查的视频。

enter image description here

答案 1 :(得分:0)

您可能想要构建视图模型

public class YourDBContext
{
    public int Id { get; set; }
    public string Name { get; set; }
}
public class YourDBContextView
{
    public int Id { get; set; }
    public string Name { get; set; }
    public bool IsSelected { get; set; }
}

并在视图

上使用@using(Html.BeginForm())
[HttpPost]
public ActionResult Index(IEnumerable<YourDBContextView> obj)
{
    //You can get your checkbox here
    foreach(YourDBContextView view in obj)
    {
        bool test = view.IsSelected;
    }
}