简体   繁体   English

通过连接数据库表过滤的MVC级联下拉列表

[英]MVC cascading dropdownlist filtered by a connecting database table

So I have a MVC on top of a SQL database and I'm creating a add to database form. 因此,我在SQL数据库之上有一个MVC,并且正在创建一个添加到数据库的表单。

I have 4 tables that concern this: 我有四个与此有关的表:

  • Club (ID, NAME) 俱乐部(ID,NAME)
  • Sport (ID, NAME) 体育(ID,NAME)
  • SportsInClub (ClubID, SportID) SportsInClub(ClubID,SportID)
  • Event (ID, NAME, CLUBID, SPORTID) // the tables contains other thing but this is what matters. 事件(ID,NAME,CLUBID,SPORTID)//表中包含其他内容,但这很重要。

So the thing is once a Club is selected by name I need to filter the Sports based on the ClubID . 因此,一旦按名称选择了Club ,我就需要根据ClubID过滤“ Sports ”。 So If I select Club AI only want sports played at club A to show up. 因此,如果我选择Club AI,则只希望显示在Club A进行的运动。

Any ideas on how to accomplish this? 关于如何做到这一点的任何想法?

As I've told you in my comment you could use JQuery and its cascading dropdown plugin but if you still want to use vanilla JS you could do something like that: 正如我在评论中告诉您的那样,您可以使用JQuery及其级联下拉插件,但是如果您仍然想使用香草JS,则可以执行以下操作:

HTML: HTML:

<select id="club" name="ClubId">
  <option selected="selected" value="?">Select a club</option>
  <option value="1">Club 1</option><!-- value is the ClubId -->
  <option value="2">Club 2</option>
  <option value="3">Club 3</option>
</select>
<select id="sport" name="SportId">
  <option value="?">Select a club first</option>
</select>

JS: JS:

var clubSelect = document.getElementById("club");
var sportSelect = document.getElementById("sport");

var resetSportsOptions = function () {
    sportSelect.options.length = 1;
    if (clubSelect.value == "?") {
        sportSelect.options[0].text = "Select a club first ";
    } else {
        sportSelect.options[0].text = "Select a sport ";
    }
}

var addSportOption = function (sport) {
    var option = document.createElement("option");
    option.text = sport.Name;
    option.setAttribute("value", sport.SportId);
    sportSelect.add(option);
}

var sendAjaxRequest = function () {
    var r = new XMLHttpRequest();
    r.open("POST", "/Sports/InClub", true);
    r.onreadystatechange = function () {
        if (r.readyState != 4 || r.status != 200) 
            return;
        var sports = JSON.parse(r.responseText);
        for (var i = 0; i < sports.length; ++i) {
            addSportOption(sports[i]);
        }
    };
    r.send("clubId=" + sportSelect.value);
}

clubSelect.addEventListener("change", function () {
    resetSportsOptions();
    if (clubSelect.value != "?") {
      sendAjaxRequest();
    }
});

ASP.NET MVC: ASP.NET MVC:

public class SportsController : Controller{
  // Your usual code
  public ActionResult InClub(int clubId){
    var sports = from s in db.Sport
                 join sic in db.SportInClub on s.SportId = sic.SportId
                 where sic.ClubId == ClubId 
                 select s
    return Json(sports);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM