gpt4 book ai didi

c# - 下拉菜单不工作 MVC C# Razor

转载 作者:行者123 更新时间:2023-11-30 15:26:50 29 4
gpt4 key购买 nike

我有一个页面,其中包含 3 个下拉菜单:客户、项目和设施。当页面第一次加载时,我能够将值加载到数据库的下拉列表中。

更改Clients中的选定值应加载基于所选客户端的新项目,同样更改所选项目应加载属于所选项目的新设施。

当我更改客户值时,项目和设施加载正常,即使所选客户未显示在下拉列表中。但是当我更改项目或设施时,似乎什么也没有发生,所有选定的值都返回 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; }
}

这是家庭 Controller

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);

我试图将选定的值保存到 session 变量中并检索它们,但它似乎不起作用。

最佳答案

所有三个下拉菜单都需要一个表单。不是每一种形式。

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

关于c# - 下拉菜单不工作 MVC C# Razor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28570328/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com