DropDownListFor与自定义生成列表

时间:2016-02-02 13:58:47

标签: asp.net-mvc asp.net-mvc-5

我想创建一个下拉列表,其价值将保存到我的数据库中。

目前,我有这个工作代码:

MyApplicationProject1
   myApplicationProject1
   myLibraryProject
   myLibraryProject1
   myLibraryProject2
   myLibraryProject3

MyApplicationProject2
   myApplicationProject2
   myLibraryProject
   myLibraryProject1
   myLibraryProject2
   myLibraryProject3

MyApplicationProject3
   myApplicationProject3
   myLibraryProject
   myLibraryProject1
   myLibraryProject2
   myLibraryProject3

但这不是正确的解决方案(代码很难看 - 我需要从2002年到2015年的几年),我想保存在我的模型中。也许这样的事情(尚未测试):

@Html.DropDownList("YearSelection",new SelectList(new [] {"2002", "2003","2004"}))
  1. 我创建的其他方法,但我不知道如何将其称为正确:
  2. 型号:

    @Html.DropDownListFor(model=>model.StartYearOfFaculty,new SelectList(new [] {"2002", "2003","2004"}))
    

    控制器 - 员工/创建

     namespace DataAccess{
    
    [MetadataType(typeof(EmployeeMD))]
    public partial class Employee{
    public List <StartYearFaculty> StartYearOFFaculty { get; set; }
    }
    
    public class StartYearFaculty{
    
    public int yearOfStartFaculty { get; set; }
    }
    }
    

    在我的观点中:

    public ActionResult Create()
        {
            Employee emp = new Employee();
    
            emp.StartYearOFFaculty = new List<StartYearFaculty>();
            emp.StartYearOFFaculty.Add(new StartYearFaculty() { yearOfStartFaculty = 2002 });
            emp.StartYearOFFaculty.Add(new StartYearFaculty() { yearOfStartFaculty = 2003 });
            emp.StartYearOFFaculty.Add(new StartYearFaculty() { yearOfStartFaculty = 2004 });
            emp.StartYearOFFaculty.Add(new StartYearFaculty() { yearOfStartFaculty = 2005 });
            emp.StartYearOFFaculty.Add(new StartYearFaculty() { yearOfStartFaculty = 2006 });
            emp.StartYearOFFaculty.Add(new StartYearFaculty() { yearOfStartFaculty = 2007 });
            emp.StartYearOFFaculty.Add(new StartYearFaculty() { yearOfStartFaculty = 2008 });
            emp.StartYearOFFaculty.Add(new StartYearFaculty() { yearOfStartFaculty = 2009 });
            emp.StartYearOFFaculty.Add(new StartYearFaculty() { yearOfStartFaculty = 2010 });
            emp.StartYearOFFaculty.Add(new StartYearFaculty() { yearOfStartFaculty = 2011 });
            emp.StartYearOFFaculty.Add(new StartYearFaculty() { yearOfStartFaculty = 2012 });
            emp.StartYearOFFaculty.Add(new StartYearFaculty() { yearOfStartFaculty = 2013 });
            emp.StartYearOFFaculty.Add(new StartYearFaculty() { yearOfStartFaculty = 2014 });
            emp.StartYearOFFaculty.Add(new StartYearFaculty() { yearOfStartFaculty = 2015 });
            var a = emp.StartYearOFFaculty;
    
            ViewBag.StartYearFaculty = emp.StartYearOFFaculty;
           return View();
        }
    

    因此是关于IEnumerable类型的错误:

      

    CS1928:'System.Web.Mvc.HtmlHelper'不包含'DropDownListFor'的定义和最佳扩展方法重载'System.Web.Mvc.Html.SelectExtensions.DropDownListFor(System.Web.Mvc.HtmlHelper, System.Linq.Expressions.Expression&gt;,System.Collections.Generic.IEnumerable)'有一些无效的参数

1 个答案:

答案 0 :(得分:2)

首先 - 你真的不应该在你的View上使用你的DAL类。对于DropDownListFor帮助程序,最好使用SelectListItem类进行数据收集。

但是,如果您仍想使用自定义类,则应指定集合中的TextValue字段,如下所示:

@Html.DropDownListFor(m => m.yearOfStartFaculty 
, new SelectList(Model.StartYearOFFaculty,"yearOfStartFaculty","yearOfStartFaculty"));

请注意,您在视图开头定义的模型必须具有yearOfStartFaculty属性。你现在没有。此属性应为stringint,并代表所选值。

但同样最好创建单独的ViewModel并创建一个字段:

public List <SelectListItem> StartYearOFFaculty { get; set; }

而不是:

public List <StartYearFaculty> StartYearOFFaculty { get; set; }

所以你的ViewModel课程看起来像这样:

public class ViewModel
{
   public List<SelectListItem> StartYearOFFaculty { get; set; }
   public int SelectedYear { get; set; }
}

如果您这样做并将其用作控制器,您将能够轻松完成:

@Html.DropDownListFor(m => m.SelectedYear, Model.StartYearOFFaculty);