ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

下拉菜单不起作用MVC C#Razor

2019-10-28 17:15:14  阅读:247  来源: 互联网

标签:razor asp-net-mvc-4 c asp-net-mvc


我有一个页面,其中包含3个下拉列表,客户,项目和设施.
当页面首次加载时,我能够将值从数据库加载到下拉列表中.

在客户端中更改所选值应基于所选客户端加载新项目,更改所选项目应以相同方式加载属于所选项目的新设施.

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

标签:razor,asp-net-mvc-4,c,asp-net-mvc
来源: https://codeday.me/bug/20191028/1954062.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有