3
我有一個頁面,有3個下拉列表,客戶,項目和設施。 當第一次加載頁面時,我可以將這些值加載到數據庫的下拉列表中。下拉不工作MVC C#剃刀
更改客戶端中的選定值應該基於所選客戶端加載新項目,改變所選項目的相同方式應加載屬於選定項目的新設施。
當我更改客戶端值時,項目和設施加載正常,甚至認爲所選客戶端未顯示在下拉列表中。但是當我改變項目或設施似乎沒有發生時,所有選定的值都會返回0。 這裏是CSHTML代碼
@using (Html.BeginForm("Index", "Home")) {
@Html.DropDownListFor(x => x.SelectedClientId, new SelectList(Model.Clients, "PrimaryKey", "Name", Model.SelectedClientId), "--Select Client--", new { onchange = "this.form.submit();" });
}
@using (Html.BeginForm("Index", "Home")) {
@Html.DropDownListFor(y => y.SelectedProjectId, new SelectList(Model.Projects, "PrimaryKey", "Name", Model.SelectedProjectId), "--Select Project--", new { onchange = "this.form.submit();" })
}
@using (Html.BeginForm("Index", "Home")) {
@Html.DropDownListFor(z => z.SelectedFacilityId, new SelectList(Model.Facilities, "PrimaryKey", "Name", Model.SelectedFacilityId), "--Select Facility--", new { onchange = "this.form.submit();" })
}
這裏是IndexViewModel
public class IndexViewModel {
public int SelectedClientId { get; set; }
public BusinessEntityCollection<Client> Clients { get; set; }
public int SelectedProjectId { get; set; }
public BusinessEntityCollection<Project> Projects { get; set; }
public int SelectedFacilityId { get; set; }
public BusinessEntityCollection<Facility> Facilities { get; set; }
}
這裏是HomeController的
public ActionResult Index(IndexViewModel model) {
BusinessEntityCollection<Client> clients = new BusinessEntityCollection<Client>();
BusinessEntityCollection<Project> projects = new BusinessEntityCollection<Project>();
BusinessEntityCollection<Facility> facilities = new BusinessEntityCollection<Facility>();
clients = ClientController.GetClients();
if (Request.HttpMethod == "POST") {
Session["SelectedClientId"] = model.SelectedClientId;
Session["SelectedProjectId"] = model.SelectedProjectId;
Session["SelectedFacilityId"] = model.SelectedFacilityId;
if (Session["SelectedClientId"] == null) {
projects.Add(new Project() { Name = "None", PrimaryKey = 0 });
}
else {
if (Convert.ToInt32(Session["SelectedClientId"]) == 0) {
projects = ProjectController.GetUserProjects(clients[0].PrimaryKey);
Session["SelectedClientId"] = clients[0].PrimaryKey;
}
else
projects = ProjectController.GetUserProjects(Convert.ToInt32(Session["SelectedClientId"]));
}
if (Session["SelectedProjectId"] == null) {
facilities.Add(new Facility() { Name = "None", PrimaryKey = 0 });
}
else {
if (Convert.ToInt32(Session["SelectedProjectId"]) != 0)
facilities = FacilityController.GetFacilitiesByProject(Convert.ToInt32(Session["SelectedProjectId"]));
}
}
model.Clients = clients; model.Projects = projects; model.Facilities = facilities;
return View(model);
}
我想選擇的值保存到會話變量和檢索他們,但它似乎並沒有工作。
謝謝,讓我試試吧 – 2015-02-17 20:22:47
真棒,它的工作,不敢相信我花了整整一天來處理這個問題。 – 2015-02-17 20:31:20