Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MVC下拉列表问题_C#_Asp.net_Asp.net Mvc - Fatal编程技术网

C# MVC下拉列表问题

C# MVC下拉列表问题,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我花了很多年的时间在经典的asp中工作,终于尝试进入.net世界。我有多个项目需要帮助 original code @Html.DropDownList("RoleID", null, htmlAttributes: new { @class = "form-control" }) new code @Html.DropDownList("RoleID", null, htmlAttributes: new { @class = "form-control", @onchange=

我花了很多年的时间在经典的asp中工作,终于尝试进入.net世界。我有多个项目需要帮助

original code 
@Html.DropDownList("RoleID", null, htmlAttributes: new { @class = "form-control" }) 

new code    
@Html.DropDownList("RoleID", null, htmlAttributes: new { @class = "form-control", @onchange="displayOptions(this.value)"})  
1) “创建”视图中有一个下拉列表。现在,它填充了表中的所有记录,但只应显示其中的一些记录。我有一个存储过程,通常用于填充下拉列表,但不知道如何在代码中更改它以使用该过程。该过程的类显示

namespace AMS_MVC.Models
{
    using System;

    public partial class usp_ListRoles_Result
    {
        public byte RoleID { get; set; }
        public string RoleName { get; set; }
    }
}
original code 
@Html.DropDownList("RoleID", null, htmlAttributes: new { @class = "form-control" }) 

new code    
@Html.DropDownList("RoleID", null, htmlAttributes: new { @class = "form-control", @onchange="displayOptions(this.value)"})  
下面是控制器现在(由VisualStudio生成)使用实体

ViewBag.RoleID = new SelectList(db.Roles, "RoleID", "RoleName");
original code 
@Html.DropDownList("RoleID", null, htmlAttributes: new { @class = "form-control" }) 

new code    
@Html.DropDownList("RoleID", null, htmlAttributes: new { @class = "form-control", @onchange="displayOptions(this.value)"})  
2) 我不希望列表中的第一项成为默认项,我更希望第一项显示为“进行选择”,而不具有值。因此,管理用户不能因为忘记进行选择而犯错误并将人员分配到错误的角色

original code 
@Html.DropDownList("RoleID", null, htmlAttributes: new { @class = "form-control" }) 

new code    
@Html.DropDownList("RoleID", null, htmlAttributes: new { @class = "form-control", @onchange="displayOptions(this.value)"})  
3) 我需要在下拉列表中添加一个onChange事件。根据选择的角色,可能会显示其他选项。例如,如果选择了部门经理的角色,则应显示部门的下拉列表;如果选择了地区经理的角色,则应显示地区列表等。。在一个选项“地点部门经理”中,它应显示部门列表和地点列表

original code 
@Html.DropDownList("RoleID", null, htmlAttributes: new { @class = "form-control" }) 

new code    
@Html.DropDownList("RoleID", null, htmlAttributes: new { @class = "form-control", @onchange="displayOptions(this.value)"})  
在经典asp中,onchange事件调用我编写的javascript函数,该函数显示正确的隐藏div

original code 
@Html.DropDownList("RoleID", null, htmlAttributes: new { @class = "form-control" }) 

new code    
@Html.DropDownList("RoleID", null, htmlAttributes: new { @class = "form-control", @onchange="displayOptions(this.value)"})  

请大家注意,一周来,我一直在这个网站上以及谷歌和必应搜索提供的其他网站上查看许多不同的例子,但他们没有任何帮助。如果您想做的只是指向其他人的问题,这些问题的答案与我的答案类似,但并不完全符合我的要求,那么请不要提供链接,因为这对我没有帮助。

在MVC中,有一个
HtmlHelper
类中的
DropDownListFor
方法。只需将其与模型属性一起使用(该属性的类型必须为
List

original code 
@Html.DropDownList("RoleID", null, htmlAttributes: new { @class = "form-control" }) 

new code    
@Html.DropDownList("RoleID", null, htmlAttributes: new { @class = "form-control", @onchange="displayOptions(this.value)"})  

@Html.DropDownListFor(m=>m.Field,Model.List,new{@id=“id”,“class=“css class”})
,其中
m.Field
是模型中存储选定值的属性,
Model.List
是显示项的列表。也可以使用“经典asp”创建此应用程序,谢谢。这回答了我问题的第三部分,关于将onchange事件添加到列表中

original code 
@Html.DropDownList("RoleID", null, htmlAttributes: new { @class = "form-control" }) 

new code    
@Html.DropDownList("RoleID", null, htmlAttributes: new { @class = "form-control", @onchange="displayOptions(this.value)"})  
对于第2部分如何在列表的开头添加另一个选项,我感到很困惑

original code 
@Html.DropDownList("RoleID", null, htmlAttributes: new { @class = "form-control" }) 

new code    
@Html.DropDownList("RoleID", null, htmlAttributes: new { @class = "form-control", @onchange="displayOptions(this.value)"})  

original code 
@Html.DropDownList("RoleID", null, htmlAttributes: new { @class = "form-control" }) 

new code    
@Html.DropDownList("RoleID", null, htmlAttributes: new { @class = "form-control", @onchange="displayOptions(this.value)"})  

original code 
@Html.DropDownList("RoleID", null, htmlAttributes: new { @class = "form-control" }) 

new code    
@Html.DropDownList("RoleID", null, htmlAttributes: new { @class = "form-control", @onchange="displayOptions(this.value)"})  

我所说的经典asp不是指asp.net,而是指asp 3.0,它与asp.net完全不同

阅读一些“如何使用HtmlHelper填充dropdowlist”mvc教程(这里是一个示例),对于第3点,您可以执行与“classic asp”相同的操作。该链接对我绝对没有帮助。对于第1项,这取决于您与数据源的连接方式。对于项目2,请选中。对于项目3,您可以遵循或使用实体framework@MirandaJohnson-您感到不安是因为您希望ASP.NET MVC像您以前的工作方式一样工作。这样不行。您需要进一步了解ASP.NET MVC的工作方式,这正是人们试图向您指出的。仅仅给你一个例子对你来说是行不通的,因为没有一个例子能确切地说明你需要什么,你必须充分理解它才能适应一个例子。你可以在MVC帮助对象中使用名为“option string”的参数来指定默认(未选择)状态。例如
@Html.DropDownListFor(m=>m.Field,Model.List,“进行选择”,new{@id=“id”,@class=“css class”})
original code 
@Html.DropDownList("RoleID", null, htmlAttributes: new { @class = "form-control" }) 

new code    
@Html.DropDownList("RoleID", null, htmlAttributes: new { @class = "form-control", @onchange="displayOptions(this.value)"})