Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.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# 在下拉列表中始终在底部具有特定ID_C#_Asp.net Mvc 3_Drop Down Menu - Fatal编程技术网

C# 在下拉列表中始终在底部具有特定ID

C# 在下拉列表中始终在底部具有特定ID,c#,asp.net-mvc-3,drop-down-menu,C#,Asp.net Mvc 3,Drop Down Menu,我有一个联系人表单的主题下拉列表。我总是希望另一个在名单的最后。我问这个的原因是因为我创建了一个控制器,所以如果我想添加更多的主题,我可以这样做。不管怎样,我可以提供一些建议或方法来确保另一个主题的位置总是在列表的底部 public void PrepareSubjectCombo() { // Grab a list of subjects for the dialog box IRepository<Subject>

我有一个联系人表单的主题下拉列表。我总是希望另一个在名单的最后。我问这个的原因是因为我创建了一个控制器,所以如果我想添加更多的主题,我可以这样做。不管怎样,我可以提供一些建议或方法来确保另一个主题的位置总是在列表的底部

public void PrepareSubjectCombo()
        {
            // Grab a list of subjects for the dialog box
            IRepository<Subject> subjectRepository = new Repository<Subject>();
            List<Subject> subjects = subjectRepository.GetAll().OrderBy(t => t.Position).ToList();
            subjects.Insert(0, new Subject() { ID = 0, Name = "- Please Select -" });
            ViewData["Subjects"] = subjects;
        }
public void PrepareSubjectCombo()
{
//获取对话框的主题列表
IRepository subjectRepository=新存储库();
List subjects=subjectRepository.GetAll().OrderBy(t=>t.Position.ToList();
插入(0,新主题(){ID=0,Name=“-请选择-”});
ViewData[“受试者”]=受试者;
}

如果有任何帮助,我已经发布了我的组合框

修改您的代码,如下所示:

DropDownList1.DataSource = YourDataSource;
DropDownList1.DataBind();

//Insert Other subject to the end
ListItem litem = new ListItem(" Other subject ", "value");
DropDownList1.Items.Insert(DropDownList1.Items.Count, litem);
public void PrepareSubjectCombo()
{
    // Grab a list of subjects for the dialog box
    IRepository<Subject> subjectRepository = new Repository<Subject>();
    List<Subject> subjects = subjectRepository.GetAll().OrderBy(t => t.Position).ToList();
    subjects.Insert(0, new Subject() { ID = 0, Name = "- Please Select -" });
    // Add this line
    // Make sure you use the correct ID for the "Other" subject
    subjects.Add(new Subject() { ID = 100, Name = "Other" });
    // If you determine the ID based on the list, simply set the ID = subjects.Count + 1
    int otherID = subjects.Count + 1;
    subjects.Add(new Subject() { ID = otherID, Name = "Other" });
    ViewData["Subjects"] = subjects;
}
public void PrepareSubjectCombo()
{
//获取对话框的主题列表
IRepository subjectRepository=新存储库();
List subjects=subjectRepository.GetAll().OrderBy(t=>t.Position.ToList();
插入(0,新主题(){ID=0,Name=“-请选择-”});
//添加这一行
//确保为“其他”主题使用正确的ID
添加(新主题(){ID=100,Name=“Other”});
//如果根据列表确定ID,只需设置ID=subjects.Count+1
int otherID=受试者。计数+1;
添加(新主题(){ID=otherID,Name=“Other”});
ViewData[“受试者”]=受试者;
}

在视图中呈现下拉列表时,只需使用DropDownList帮助程序的适当重载,而不是尝试将一些虚拟项插入集合:

public void PrepareSubjectCombo()
{
    // Grab a list of subjects for the dialog box
    IRepository<Subject> subjectRepository = new Repository<Subject>();
    List<Subject> subjects = subjectRepository.GetAll().OrderBy(t => t.Position).ToList();
    ViewData["Subjects"] = subjects;
}

-1.OP询问有关ASP.NET MVC的信息。您所展示的内容与MVC无关。这是标准的WebForms代码。您正在显示一些数据源、数据绑定、服务器端控件等。。。ASP.NET MVC中不存在的所有内容。
@Html.DropDownList(
    "selectedSubject", 
    new SelectList(ViewData["Subjects"] as List<Subject>, "ID", "Name"), 
    "- Please Select -"
)
public class MyViewModel
{
    public string SelectedSubject { get; set; }
    public IEnumerable<SelectListItem> Subjects { get; set; }
}
public ActionResult Index()
{
    // TODO: You definitively don't want to hardcode your repository like this
    // but use a constructor injection or this action will be impossible to unit test
    IRepository<Subject> subjectRepository = new Repository<Subject>();
    var subjects = subjectRepository.GetAll().OrderBy(t => t.Position).ToList();
    var model = new MyViewModel
    {
        Subjects = subjects.Select(x => new SelectListItem
        {
            Value = x.ID.ToString(),
            Text = x.Name
        })
    };   
    return View(model);
}
@model MyViewModel
@Html.DropDownListFor(
    x => x.SelectedSubject, 
    new SelectList(Model.Subjects, "Value", "Text"), 
    "- Please Select -"
)