2011-02-26 19 views
3

我需要在窗體中使用簡單的DropDownList,我不想創建類似ViewModel的東西。 我有兩個模型(表)就1:N:ASP.NET MVC3中的簡單DropDownList應用程序

public class Course 
{ 
    public int ID { get; set; } 
    public string Name { get; set; } 
} 

public class Project 
{ 
    public int ID { get; set; } 
    public int CourseId { get; set; } 
    public int ProjectNo { get; set; } 
    public string Name { get; set; } 
    public DateTime Deadline { get; set; } 
} 

在 '創建工程' 我想擁有的DropDownList與ID(如值)和名稱(如文字)從課程表(模型)。在新項目中將插入選定的CourseId。我該如何儘可能簡單地做到這一點?

回答

5

你不想使用ViewModel的任何特殊原因?他們對這類問題非常有幫助。

如果你不想使用視圖模型,那麼你可以構建你的控制器特定的類,它是從兩個類所需要的屬性的集合:

public ActionResult Show(int id) 
{ 
    Course course = repository.GetCourse(id); // whatever your persistence logic is here 
    Project project = projectRepository.GetProjectByCourseId(id); 
    string CourseName = from c in course where 
          c.ID == project.courseID 
          select c.Name; 
    IEnumerable<SelectListItem> selectList = 
    from c in course 
    select new SelectListItem 
    { 
     Selected = (c.ID == project.CourseId), 
     Text = c.Name, 
     Value = project.CourseId.ToString() 
    }; 
    //add the selectList to your model here. 
    return View(); //add the model to your view and return it. 
} 

這將是容易得多爲此有一個ViewModel,所以你可以有一個強類型的視圖。讓我告訴你:

public class ProjectCourseViewModel 
{ 
    public SelectList ProjectCourseList {get; private set; } 
    public Project Project {get; private set; } 
    public Course Course {get; private set; } 

    public ProjectCourseViewModel(Project project, Course course) 
    { 
     ProjectCourseList = GetProjectCourseSelectList(project, course) 
     Project = project; 
     Course = course; 
    } 

    private SelectList GetProjectCourseSelectList(Project project, Course course) 
    { 
     IEnumerable<SelectListItem> selectList = 
     from c in course 
     select new SelectListItem 
     { 
      Selected = (c.ID == project.CourseId), 
      Text = c.Name, 
      Value = project.CourseId.ToString() 
     }; 
    } 

} 

然後您的控制器將是非常簡單的:

public ActionResult Show(int id) 
{ 
    Course course = repository.GetCourse(id); 
    Project project = projectRepository.GetProjectByCourseId(id); 
    ProjectCourseViewModel pcvm = new ProjectCourseViewModel(project, course) 
    return View(pcvm); 
} 

然後你的看法發生在一個強類型的模型,你不必依靠ViewData,這是一件好事。

注:我沒有編譯過,只是寫了它。有可能是編譯錯誤。

2

或許你可以用下面的例子中解決這個問題:

在控制器

包括Viewbag

{ 
Viewbag.Course = db.course.ToList(); 
var project = new project..... 
} 

並且在視圖使用以下模式:

@Html.DropDownList("CourseId", 
    new SelectList(ViewBag.Course as System.Collections.IEnumerable, 
    "CourseId", "Name", Model.ID)) 

其中,每個字段代表:

•表單字段的名稱(CourseId)

•值的下拉列表,爲的SelectList

•應當調回與形式

•數據文本字段應顯示在下拉列表中的數據值的字段通過名單

•所選值時,將顯示

更多信息的形式被用於設置下拉列表值:http://www.asp.net/mvc/tutorials/mvc-music-store-part-5 brgds。

2

在的Controler:

var CourseName = from c in course where 
          c.ID == project.courseID 
          select c.Name; 

    SelectList sl = new SelectList(CourseName); 

ViewBag.names= sl; 

在視圖

@Html.DropDownList("Name", (SelectList)ViewBag.names)