0

所以我在 ASP.NET MVC4 中工作,我想动态加载一个下拉列表。

这是我的模型类:

public class Meeting
{
public int MeetingId { get; set; }
[Required(ErrorMessage = "A name is required")]
public String Name { get; set; }
[Required(ErrorMessage = "Location is required")]
public String Location { get; set; }
[Required(ErrorMessage = "A start date is required")]
[DataType(DataType.Date)]
public DateTime StartDate { get; set; }
[DataType(DataType.Date)]
[DateGreaterThan("StartDate")]
public DateTime? EndDate  { get; set; }

public int? CountryId { get; set; }
public virtual Country Country { get; set; }
public String Sport { get; set; }

public List<string> SelectedTeamIds { get; set; }
public virtual List<TeamViewModel> SelectedTeams { get; set; }

public List<int> SelectedEventIds { get; set; }
public virtual List<EventViewModel> SelectedEvents { get; set; }



   public Meeting()
   {
    SelectedTeamIds = new List<string>();
    SelectedTeams = new List<TeamViewModel>();

    SelectedEventIds = new List<int>();
    SelectedEvents = new List<EventViewModel>(); 
    }
}

这是我感兴趣的控制器方法:

public ActionResult Create()
{
  ViewBag.CountryId = new MultiSelectList(db.Countries, "CountryId", "CountryName");
  ViewBag.AvailableEvents = db.Events.Where(e => e.SchoolcupEvent == true);
  ViewBag.AvailableTeams = db.Teams;
  var model = new Meeting();
  return View(model);
}

这是此方法的视图:

@model Project.Domain.POCO.Meeting

<h2>Create</h2>
 @using (Html.BeginForm()) {
@Html.ValidationSummary(true)

<fieldset>
    <legend>Meeting</legend>
    <div class="editor-label">
        @Html.LabelFor(model => model.Name)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Name)
        @Html.ValidationMessageFor(model => model.Name)
    </div>
    <div class="editor-label">
        @Html.DisplayName("Country")
    </div>
    <div class="editor-field">
        @Html.DropDownList("CountryId", "Pick a country")
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.Location)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Location)
        @Html.ValidationMessageFor(model => model.Location)
    </div>
    <div class="editor-label">
        @Html.DisplayName("Start Date")
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.StartDate)
        @Html.ValidationMessageFor(model => model.StartDate)
    </div>
    <div class="editor-label">
        @Html.DisplayName("End Date")
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.EndDate)
        @Html.ValidationMessageFor(model => model.EndDate)
    </div>
     <div class="editor-label">
        @Html.Label("Sports type")
    </div>
    <div class="editor-field">
        @Html.RadioButtonFor(m => m.Sport, "Swimming" ) Swimming
        @Html.RadioButtonFor(m => m.Sport, "Athletics" ) Athletics
    </div>

    <div class="editor-label">
        @Html.Label("Select Event(s)")
    </div>
    <div class="editor-field">
        @Html.ListBoxFor(model => model.SelectedEventIds, new MultiSelectList(ViewBag.AvailableEvents, "EventId", "Discipline", Model.SelectedEventIds), new { @class = "superselect" })
    </div>
    <div class="editor-label">
        @Html.Label("Select Team(s)")
    </div>
    <div class="editor-field">
        @Html.ListBoxFor(model => model.SelectedTeamIds, new MultiSelectList(ViewBag.AvailableTeams, "TeamId", "TeamId", Model.SelectedTeamIds), new { @class = "superselect" })
    </div>
    <p class="Button">
        <input type="submit" />
    </p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
@Styles.Render("~/Content/Css/UserInterface.css")
}

会议由许多事件组成。所以问题出在带有事件的 ListBox 上,我怎样才能只动态加载属于我之前选择的会议的事件。我已经对全能的 Google 进行了一些研究,但我得到的只是涉及 javascript/jquery 的答案,而且由于我对此的了解基本上是 0,因此我希望如果可能的话以另一种方式解决这个问题。如果没有,您能否简单地向我解释一下我需要做什么以及我在做什么。提前致谢。

4

2 回答 2

1

我认为没有 jquery 或 javascript 就无法实现它。如果要使用 ASP.NET MVC,您应该熟悉 jquery,它至关重要。对于您这里的一些提示可能会有所帮助:

  • 第一个下拉菜单的选择更改是 jquery 可以捕获的事件
  • jquery 函数可以将选定的值发布到服务器并接收第二个下拉列表的相关数据。
  • 第二个下拉列表可以通过将选项附加到空下拉列表来填充。如果第二个下拉列表取决于第一个下拉列表的选择,则不必首先填写它。但是每次第一个下拉列表的选择发生变化时,您都必须删除所有项目(选项)

但是,还有另一个称为 ASP.NET Webforms 的选项,您可以在 VS 工具的帮助下定义事件处理。

于 2013-05-17T10:39:06.980 回答
0

您可以一起避免使用 jquery 并使用 LiteralControl 类。

ASP.NET MVC 的 LiteralControl 类。

于 2015-12-09T13:10:43.737 回答