下拉列表中的导航属性

时间:2017-10-11 20:27:44

标签: c# asp.net-mvc repository-pattern

我目前正在学习界面和存储库模式,并且在我的简单项目中遇到了CRUD操作的困难。

我有域类“VehicleMake”

public class VehicleMake
{
    public VehicleMake()
    {

    }

    [Key]
    [Column(Order = 1)]
    public int Id { get; set; }

    [Required]
    [StringLength(50)]
    public string Name { get; set; }

    public virtual ICollection<VehicleModel> VehicleModels { get; set; }
}

和域类“VehicleModel”

public class VehicleModel
{
    [Key]
    [Column(Order = 1)]
    public int Id { get; set; }

    public int VehicleMakeId { get; set; }

    [Required]
    [StringLength(50)]
    public string Name { get; set; }

    public virtual VehicleMake VehicleMake { get; set; }
}

我还有一个接口“IVehicleRepository”

  public interface IVehicleRepository
{
    void Add(VehicleMake vehicleMake);

    IEnumerable<VehicleMake> AllMakes { get; }
    IEnumerable<VehicleModel> AllModels { get; }
}

和实现该接口的类“VehicleRepository”

 public class VehicleRepository : IVehicleRepository
{
    private readonly VehicleDbContext _context;

    public VehicleRepository(VehicleDbContext context)
    {
        _context = context;
    }

    public void Add(VehicleMake vehicleMake)
    {
        _context.VehicleMakes.Add(vehicleMake);
        _context.SaveChanges();
    }

    public IEnumerable<VehicleMake> AllMakes => _context.VehicleMakes;
    public IEnumerable<VehicleModel> AllModels => _context.VehicleModels;
}

我的DbContext类正在关注

public class VehicleDbContext : DbContext
{
    public VehicleDbContext() : base("VehicleDbContext")
    {

    }

    public DbSet<VehicleMake> VehicleMakes { get; set; }
    public DbSet<VehicleModel> VehicleModels { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {

    }

}

我的“VehicleMakeViewModel”正在关注

public class VehicleMakeViewModel
{
    [Required]
    [Display(Name = "Vehicle Make Number")]
    public int Id { get; set; }

    [Required]
    [Display(Name = "Vehicle Make Name")]
    public string Name { get; set; }
}

我的“VehicleModelViewModel”正在关注

  public class VehicleModelViewModel
{
    [Required]
    [Display(Name = "Vehicle Model Number")]
    public int Id { get; set; }

    [Required]
    [Display(Name = "Vehicle Model Name")]
    public string Name { get; set; }

    [Required]
    [Display(Name = "Vehicle Make Id")]
    public int VehicleMakeId { get; set; }
}

我有“VehicleMakeController”

 public class VehicleMakeController : Controller
{
    private readonly IVehicleRepository _vehicleRepository;

    public VehicleMakeController()
    {
        _vehicleRepository = new VehicleRepository(new VehicleDbContext());
    }

    // GET: VehicleMake
    public ActionResult Index()
    {
        return View(_vehicleRepository.AllMakes);
    }

    public ActionResult CreateVehicleMake()
    {
        return View(new VehicleMakeViewModel());
    }

    [HttpPost]
    public ActionResult CreateVehicleMake(VehicleMakeViewModel viewModel)
    {
        if (ModelState.IsValid)
        {
            var vehicleMake = new VehicleMake();
            UpdateVehicleMake(vehicleMake, viewModel);
            _vehicleService.Add(vehicleMake);
            return RedirectToAction("Index");
        }

        return View(viewModel);
    }

    private void UpdateVehicleMake(VehicleMake vehicleMake, VehicleMakeViewModel viewModel)
    {
        vehicleMake.Id = viewModel.Id;
        vehicleMake.Name = viewModel.Name;
    }
}

和“VehicleModelController

 public class VehicleModelController : Controller
{

    private readonly IVehicleRepository _vehicleRepository;

    public VehicleModelController()
    {
        _vehicleRepository = new VehicleRepository(new VehicleDbContext());
    }

    // GET: VehicleModel
    public ActionResult Index()
    {
        return View(_vehicleRepository.AllModels);
    }


}

正如你在我的“VehicleMakeController”中看到的,我有添加新车辆制作“CreateVehicleMake”的方法。问题是我不知道如何在车辆模型的“VehicleModelController”中这样做。当用户想要添加新车型时,他应该在文本框中输入新车型的名称,然后他应该在下拉列表中选择属于该车型的车辆品牌。我不知道如何使用存储库模式实现这一点,我们将不胜感激。

1 个答案:

答案 0 :(得分:0)

public class VehicleModelViewModel { public List<SelectListItem> Makes { set;get;} [Required] [Display(Name = "Vehicle Model Name")] public string Name { get; set; } [Required] [Display(Name = "Vehicle Make Id")] public int VehicleMakeId { get; set; } } 课程添加新属性,以保存车辆制作下拉列表的可用选项列表。

Makes

现在,在创建屏幕的GET操作中,创建此视图模型的对象,从“制作”列表中加载public class VehicleModelController : Controller { private readonly IVehicleRepository _vehicleRepository; public VehicleModelController() { _vehicleRepository = new VehicleRepository(new VehicleDbContext()); } public ActionResult Create() { var vm=new VehicleModelViewModel(); vm.Makes = GetMakeOptions(); return View(vm); } private List<SelectListeItem> GetMakeOptions() { return this._vehicleRepository.AllMakes .Select(x=>new SelectListItem { Value=x.Id.ToString(), Text=x.Name}) .ToList(); } } 属性

Html.DropDownListFor

现在,在您的视图中,我们将使用@model VehicleViewModel @using(Html.BeginForm()) { @Html.LabelFor(s=>s.Name) @Html.TextBoxFor(s=>s.Name) @Html.ValidationMessageFor(s=>s.Name) @Html.LabelFor(s=>s.MakeId) @Html.DropDownListFor(s=>s.VehicleMakeId , Model.Makes) @Html.ValidationMessageFor(s=>s.VehicleMakeId) <input type="submit" /> } 辅助方法生成SELECT元素

[HttpPost]
public ActionResult Create(VehicleViewModel model)
{
  if(ModelState.IsValid)
  {
     var e = new VehicleModel { Name = model.Name, VehicleMakeId = model.VehicleMakeId };
     _vehicleRepository.Add(e);
     return RedirectToAction("Index");
  }
  model.Makes=GetMakes();
  return View(model);
}

这将使用表单内的SELECT元素呈现视图。确保在控制器中有一个HttpPost Create操作方法,该方法处理表单提交并将数据保存回表

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        tableView.separatorInset = UIEdgeInsets.zero
        tableView.layoutMargins = UIEdgeInsets.zero
        cell.preservesSuperviewLayoutMargins = false
        cell.layoutMargins = UIEdgeInsets.zero
        cell.separatorInset = UIEdgeInsets.zero
}