.net core Razor 页面中级联 html.DropDownListFor

发布于 2025-01-13 07:39:59 字数 4555 浏览 0 评论 0原文

我正处于失去理智的地步。所以为了避免这种情况,我必须问你这个问题。 现在,我知道,那里有很多“类似”的问题。相信我,过去三天我一直在研究它们,但它们都不适合我。所以我希望你能够帮助我解决这个问题。

我一直在学习 Udemy.com 上有关 ASP.NET Core 和 Razor Pages 的课程。经过一些改变,我成功创建了自己的小项目。然而我现在只缺少一件事。

我想要一个 DropDownListFor,其选择基于另一个 DropDownListFor 中的选择。

简而言之,我必须数据库表。一种称为 Team,另一种称为 RepairType。每种维修类型均由其中一个团队完成。

所以我的模型看起来像这样:

public class Team
{
    [Key]
    public int Id { get; set; }

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

public class RepairType
{
    [Key]
    public int Id { get; set; }

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

    [Required]
    [Display(Name = "Repair Team")]
    public int TeamId { get; set; }

    [ForeignKey("TeamId")]
    public virtual Team Team { get; set; }
}

我也有一个 VievModel:

public class RepairVM
{
    public Repair Repair { get; set; }
    public IEnumerable<SelectListItem> RepairTypeList { get; set; }
    public IEnumerable<SelectListItem> TeamList { get; set; }
    public IEnumerable<SelectListItem> DeckList { get; set; }
    public IEnumerable<SelectListItem> UserList { get; set; }

}

我有一些像这样的存储库:

public class TeamRepository : Repository<Team>, ITeamRepository
{
    private readonly ApplicationDbContext _db;

    public TeamRepository(ApplicationDbContext db) : base(db)
    {
        _db = db;
    }

    public IEnumerable<SelectListItem> GetTeamListForDropDown()
    {
        return _db.Team.Select(i => new SelectListItem()
        {
            Text = i.Name,
            Value = i.Id.ToString()
        });
    }

 public class RepairTypeRepository : Repository<RepairType>, IRepairTypeRepository
{
    private readonly ApplicationDbContext _db;

    public RepairTypeRepository(ApplicationDbContext db) : base(db)
    {
        _db = db;
    }

    public IEnumerable<SelectListItem> GetRepairTypeListForDropDown()
    {
        return _db.RepairType.Select(i => new SelectListItem()
        {
            Text = i.Name,
            Value = i.Id.ToString()
        });
    }

在我的 Razor 页面中,我有这两个 html.DropDownListFor:

<div class="form-group row">
                <div class="col-lg-3">
                    <label asp-for="RepairObj.Repair.TeamId"></label>
                </div>
                <div class="col-lg-9">
                    @Html.DropDownListFor(m => m.RepairObj.Repair.TeamId, Model.RepairObj.TeamList, "- Please select a Team -", new { @class = "form-control", @onchange = "javascript:GetType(this.value);" })
                    <span class="text-danger" asp-validation-for="RepairObj.Repair.TeamId"></span>
                </div>
            </div>
            <div class="form-group row">
                <div class="col-lg-3">
                    <label asp-for="RepairObj.Repair.RepairTypeId"></label>
                </div>
                <div class="col-lg-9">
                    @Html.DropDownListFor(m => m.RepairObj.Repair.RepairTypeId, Model.RepairObj.RepairTypeList, "- Please select a Repair Type -", new { @class = "form-control" })
                    <span class="text-danger" asp-validation-for="RepairObj.Repair.RepairTypeId"></span>
                </div>
            </div>

加载页面时我的代码看起来像这样:

public class CreateRepairModel : PageModel
{
    private readonly IUnitOfWork _unitofWork;
    private readonly IWebHostEnvironment _hostingEnvironment;
    


    public CreateRepairModel(IUnitOfWork unitOfWork, IWebHostEnvironment hostingEnvironment)
    {
        _unitofWork = unitOfWork;
        _hostingEnvironment = hostingEnvironment;
       
    }

    [BindProperty]
    public RepairVM RepairObj { get; set; }

    
    public IActionResult OnGet()
    {
        
        RepairObj = new RepairVM
        {                
            TeamList = _unitofWork.Team.GetTeamListForDropDown(),
            RepairTypeList = _unitofWork.RepairType.GetRepairTypeListForDropDown(),
            DeckList = _unitofWork.Deck.GetDeckListForDropDown(),
            Repair = new Models.Repair()
        };
        
        return Page();
    }

我已经尝试过javaScript、jQuery 等等,但正如我所说,没有什么真正适合我。请记住,这是我的第一个 Web 应用程序项目,所以我真的希望有人会如此友善,并准确地告诉我该怎么做,这样我在下拉列表中只会有与团队下拉列表匹配的修复类型。

我希望这一切都有意义,如果需要,我很乐意为您提供更多信息。

谢谢。

I am at the point where I am loosing my mind. So to avoid that, I have to ask you this questions.
Now, I know, that there are lot of "similar" questions out there. Believe me, I have spend the last 3 days looking at them, but none of them is working for me. So I hope you will be able to help me with this.

I have been following a course on Udemy.com about ASP.NET Core and Razor Pages. With some changes I have managed to create my little own project. However I am missing only one thing at the moment.

I would like to have a DropDownListFor with selections based on what is selected in another DropDownListFor.

In short I have to db tables. One called Team and one called RepairType. Each repairtype is done by one of the teams.

So my Model looks like this:

public class Team
{
    [Key]
    public int Id { get; set; }

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

public class RepairType
{
    [Key]
    public int Id { get; set; }

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

    [Required]
    [Display(Name = "Repair Team")]
    public int TeamId { get; set; }

    [ForeignKey("TeamId")]
    public virtual Team Team { get; set; }
}

I also have a VievModel:

public class RepairVM
{
    public Repair Repair { get; set; }
    public IEnumerable<SelectListItem> RepairTypeList { get; set; }
    public IEnumerable<SelectListItem> TeamList { get; set; }
    public IEnumerable<SelectListItem> DeckList { get; set; }
    public IEnumerable<SelectListItem> UserList { get; set; }

}

I have a few repositories that look like this:

public class TeamRepository : Repository<Team>, ITeamRepository
{
    private readonly ApplicationDbContext _db;

    public TeamRepository(ApplicationDbContext db) : base(db)
    {
        _db = db;
    }

    public IEnumerable<SelectListItem> GetTeamListForDropDown()
    {
        return _db.Team.Select(i => new SelectListItem()
        {
            Text = i.Name,
            Value = i.Id.ToString()
        });
    }

 public class RepairTypeRepository : Repository<RepairType>, IRepairTypeRepository
{
    private readonly ApplicationDbContext _db;

    public RepairTypeRepository(ApplicationDbContext db) : base(db)
    {
        _db = db;
    }

    public IEnumerable<SelectListItem> GetRepairTypeListForDropDown()
    {
        return _db.RepairType.Select(i => new SelectListItem()
        {
            Text = i.Name,
            Value = i.Id.ToString()
        });
    }

In my Razor Page, I have these two html.DropDownListFor:

<div class="form-group row">
                <div class="col-lg-3">
                    <label asp-for="RepairObj.Repair.TeamId"></label>
                </div>
                <div class="col-lg-9">
                    @Html.DropDownListFor(m => m.RepairObj.Repair.TeamId, Model.RepairObj.TeamList, "- Please select a Team -", new { @class = "form-control", @onchange = "javascript:GetType(this.value);" })
                    <span class="text-danger" asp-validation-for="RepairObj.Repair.TeamId"></span>
                </div>
            </div>
            <div class="form-group row">
                <div class="col-lg-3">
                    <label asp-for="RepairObj.Repair.RepairTypeId"></label>
                </div>
                <div class="col-lg-9">
                    @Html.DropDownListFor(m => m.RepairObj.Repair.RepairTypeId, Model.RepairObj.RepairTypeList, "- Please select a Repair Type -", new { @class = "form-control" })
                    <span class="text-danger" asp-validation-for="RepairObj.Repair.RepairTypeId"></span>
                </div>
            </div>

And my code when loading the page looks like this:

public class CreateRepairModel : PageModel
{
    private readonly IUnitOfWork _unitofWork;
    private readonly IWebHostEnvironment _hostingEnvironment;
    


    public CreateRepairModel(IUnitOfWork unitOfWork, IWebHostEnvironment hostingEnvironment)
    {
        _unitofWork = unitOfWork;
        _hostingEnvironment = hostingEnvironment;
       
    }

    [BindProperty]
    public RepairVM RepairObj { get; set; }

    
    public IActionResult OnGet()
    {
        
        RepairObj = new RepairVM
        {                
            TeamList = _unitofWork.Team.GetTeamListForDropDown(),
            RepairTypeList = _unitofWork.RepairType.GetRepairTypeListForDropDown(),
            DeckList = _unitofWork.Deck.GetDeckListForDropDown(),
            Repair = new Models.Repair()
        };
        
        return Page();
    }

I have tried javaScript, jQuery and so on, but as I said, nothing really works for me. Please have in mind that this is my first ever web application project, so I am really hoping that someone will be so kind, and tell me exactly what to do, so that I will only have the repairtype in the dropdownlist that match the teams dropdownlist.

I hope it all makes sense and I will be happy to supply you with more information if needed.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

高冷爸爸 2025-01-20 07:39:59

我们需要对选项字段使用数据属性,以便我们可以进行过滤,因此必须手动填写修复列表。请按照以下步骤操作;

  1. team-select 类添加到 Teams 下拉列表中。稍后我们将使用它来绑定我们的事件。
@Html.DropDownListFor(m => m.RepairObj.Repair.TeamId, Model.RepairObj.TeamList, "- Please select a Team -", new { @class = "form-control team-select"})
  1. 使用它作为您的修复下拉列表。在每个选项中,我都使用了数据属性; 数据团队
/* 
We have to manually fill the options because SelectListItem doesn't support data-attribute.
I also added the class repair-select for event bind.
*/

<select name="RepairObj.Repair.RepairTypeId" class="repair-select form-control">
   <option>- Please select a Repair Type -</option>
   @foreach(var type in Model.RepairObj.RepairTypeList){
      <option data-team="@type.TeamId" value="@type.Id">@type.Name</option>
   }
</select>
  1. 然后使用这个脚本。
@section scripts {
   <script>
      $(document).ready(function(){

         // bind an event everytime team select is changed
         $(".team-select").change(function(){

            // get the selected teamId
            var teamId = $(this).val();

            // loop through all option
            $(".repair-select option").each(function(){

               // get the team id from data-attribute; data-team
               var dataTeamId = $(this).data("team");

               // if the dataTeamId is equal to teamId, show it, else hide
               if(dataTeamId == teamId){
                  $(this).show();
                  $(this).removeAttr("disabled");
               }
               else{
                  $(this).hide();
                  $(this).attr("disabled",true);
               }
            });
            
            // select the first option in repairType dropdown
            $(".repair-select option").eq(0).prop("selected", true);
         });
      });
   </script>
}
  1. GetRepairTypeListForDropDown 更新为以下代码。
public List<RepairType> GetRepairTypeListForDropDown()
{
   return _db.RepairType.ToList();
}

We need to use data-attribute for the option fields so we could filter, hence the repair list has to be manually filled. Follow the steps below;

  1. Add the class team-select to Teams dropdown. We will use this to bind our event later.
@Html.DropDownListFor(m => m.RepairObj.Repair.TeamId, Model.RepairObj.TeamList, "- Please select a Team -", new { @class = "form-control team-select"})
  1. Use this as your Repair dropdown. In each option, I used data-attribute; data-team.
/* 
We have to manually fill the options because SelectListItem doesn't support data-attribute.
I also added the class repair-select for event bind.
*/

<select name="RepairObj.Repair.RepairTypeId" class="repair-select form-control">
   <option>- Please select a Repair Type -</option>
   @foreach(var type in Model.RepairObj.RepairTypeList){
      <option data-team="@type.TeamId" value="@type.Id">@type.Name</option>
   }
</select>
  1. Then use this script.
@section scripts {
   <script>
      $(document).ready(function(){

         // bind an event everytime team select is changed
         $(".team-select").change(function(){

            // get the selected teamId
            var teamId = $(this).val();

            // loop through all option
            $(".repair-select option").each(function(){

               // get the team id from data-attribute; data-team
               var dataTeamId = $(this).data("team");

               // if the dataTeamId is equal to teamId, show it, else hide
               if(dataTeamId == teamId){
                  $(this).show();
                  $(this).removeAttr("disabled");
               }
               else{
                  $(this).hide();
                  $(this).attr("disabled",true);
               }
            });
            
            // select the first option in repairType dropdown
            $(".repair-select option").eq(0).prop("selected", true);
         });
      });
   </script>
}
  1. Update GetRepairTypeListForDropDown to the code below.
public List<RepairType> GetRepairTypeListForDropDown()
{
   return _db.RepairType.ToList();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文