简体   繁体   English

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

[英]Dropdowns not working MVC C# Razor

I have a page that has 3 dropdowns, Clients, Projects and Facilities. 我有一个页面,有3个下拉菜单,客户,项目和设施。 I am able to load the values into the dropdowns from database when the page loads first time. 我可以在第一次加载页面时将值加载到数据库的下拉列表中。

Changing the selected value in Clients should load new projects based on the selected clients, same way changing the selected project should load new facilities that belongs to selected project. 更改客户端中的选定值应基于所选客户端加载新项目,更改所选项目的相同方式应加载属于所选项目的新工具。

When I change the Client value, the projects and facilities are loading fine, even thought the selected client is not shown in the dropdown. 当我更改客户端值时,项目和工具正在正常加载,即使下拉列表中未显示所选客户端。 But when I change the projects or facilities nothing seems to happen, all selected values return 0s. 但是当我改变项目或设施似乎没有发生任何事情时,所有选定的值都返回0。 Here is the cshtml code 这是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();" })
}

Here is the IndexViewModel 这是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; }
}

Here is the HomeController 这是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);

} }

I am trying to save the selected values into session variables and retrieving them, but it doesn't seem to work. 我试图将选定的值保存到会话变量并检索它们,但它似乎不起作用。

You need a single form for all three dropdowns. 所有三个下拉菜单都需要一个表单。 Not one form for each. 每个都不是一种形式。

@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