簡體   English   中英

下拉列表沒有工作MVC C#Razor

[英]Dropdowns not working MVC C# Razor

我有一個頁面,有3個下拉菜單,客戶,項目和設施。 我可以在第一次加載頁面時將值加載到數據庫的下拉列表中。

更改客戶端中的選定值應基於所選客戶端加載新項目,更改所選項目的相同方式應加載屬於所選項目的新工具。

當我更改客戶端值時,項目和工具正在正常加載,即使下拉列表中未顯示所選客戶端。 但是當我改變項目或設施似乎沒有發生任何事情時,所有選定的值都返回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);

}

我試圖將選定的值保存到會話變量並檢索它們,但它似乎不起作用。

所有三個下拉菜單都需要一個表單。 每個都不是一種形式。

@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();" });

    @Html.DropDownListFor(y => y.SelectedProjectId, new SelectList(Model.Projects, "PrimaryKey", "Name", Model.SelectedProjectId), "--Select Project--", new { onchange = "this.form.submit();" })

    @Html.DropDownListFor(z => z.SelectedFacilityId, new SelectList(Model.Facilities, "PrimaryKey", "Name", Model.SelectedFacilityId), "--Select Facility--", new { onchange = "this.form.submit();" })
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM