Simple DropDownList in ASP.NET MVC3 Application

I need a simple DropDownList in the form, and I don't want to create something like a ViewModel. I have two models (tables) with respect to 1: n:

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

and

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; }
}

In "Create Project" I want to have a DropDownList with the identifier (as value) and name (as text) from the course table (model). In the new project, the selected CourseId will be inserted. How can I make this as simple as possible?

+3
source share
3 answers

Any specific reason you don't want to use ViewModel? They are very useful for this type of problem.

If you do not want to use ViewModel, you can create a specific class in your controller, which is a collection of properties required for both classes:

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, .

. , . , .

+5

, , :

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

• Data Value,

• " ",

• ,

: http://www.asp.net/mvc/tutorials/mvc-music-store-part-5 brgds.

+2

In the controller:

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

  SelectList sl = new SelectList(CourseName);

 ViewBag.names= sl;

in view:

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

Source: https://habr.com/ru/post/1794954/


All Articles