所以我在 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,因此我希望如果可能的话以另一种方式解决这个问题。如果没有,您能否简单地向我解释一下我需要做什么以及我在做什么。提前致谢。