0

My task is to create a .NET MVC Form connected to local MS SQL server. The form should care for registering data for Students and Courses. In the DB there is simple tables Students, StudentsCourses, Courses, and Teachers. Since I have to gain information for more than one Model, I'd created a ViewModel, Here it is :

public class ViewModelVM
    {
        public Teacher Teacher { get; set; }
        public Student Student { get; set; }
        public Cours Cours { get; set; }
    }

In the database, I've already have some Courses, and my goal is to create DropDownList, so the Students must choose among them.

@using (Html.BeginForm())
{
    @Html.ValidationSummary()
    <div class="form-group">
        <label>Student Firstname:</label>
        @Html.TextBoxFor(x => x.Student.FirstName, new { @class = "formcontrol" })
    </div>
    <div class="form-group">
        <label>Student Birthdate:</label>
        @Html.TextBoxFor(x => x.Student.BirthDate, new { @class = "formcontrol" })
    </div>
    <div class="form-group">
        <label>Student FNum:</label>
        @Html.TextBoxFor(x => x.Student.Fnum, new { @class = "formcontrol" })
    </div>


  **<div class="form-group">
            <label>Studet's Courses</label>
            // I want to make something like this
            // @Html.DropDownListFor(x => x.Student.Courses, new[] {
 new SelectListItem() {Text = "C++", Value = bool.TrueString},
 new SelectListItem() {Text = "c#", Value = bool.FalseString}}, 
                        "Choose course", new { @class = "formcontrol" })

        </div>**


    <div class="form-group">
        <label>Teacher Fnum:</label>
        @Html.TextBoxFor(x => x.Teacher.Fnum, new { @class = "formcontrol" })
    </div>
    <div class="form-group">
        <label>Course Signature:</label>
        @Html.TextBoxFor(x => x.Cours.Signature, new { @class = "formcontrol" })
    </div>
    <div class="form-group">
        <label>Course.Teacher Fnum data:</label>
        @Html.TextBoxFor(x => x.Cours.Teacher.Fnum, new { @class = "formcontrol" })
    </div>
    <div class="text-center">
        <input class="btn btn-success" type="submit"
               value="Submit Info" />
    </div>
}

Important! : Items in the DropDownList wont be new, they will exist in the Database already.

My question is how exactly I can write this DropDown, because it is not correct now. Value = bool.TrueString, is just example text, i would like to have something like : Model.Student.Courses.Where(CourseName == 'C++');

Pete
  • 57,112
  • 28
  • 117
  • 166
FarCry88
  • 15
  • 5
  • SO what is your question? –  Sep 01 '15 at 13:05
  • My question is how exactly I can write this DropDown, because it is not correct now. Value = bool.TrueString, is just example text, i would like to have something like : Model.Student.Courses.Where(CourseName == 'C++'); – FarCry88 Sep 01 '15 at 13:08
  • 2
    Then create a `SelectList` in the controller (by calling your database and populating it). And before you go any further, i suggest you read [What is ViewModel in MVC?](http://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) to understand what a view model is –  Sep 01 '15 at 13:11

1 Answers1

0

Here is a model based on MVC Music Store's example. In your view, use the following code:

@Html.DropDownList("DropDownStudent", "Select student", new { @class = "formcontrol" })

Now you need to fill the combobox with values using the ViewData in the action method inside the controller. For example:

public ActionResult Index()
{   
    var List<string> students= new List<string>();
    students.Add("Amanda");
    students.Add("Heather");
    students.Add("Jane");
    students.Add("Jonh");
    students.Add("Mark");         

    ViewBag.DropDownStudent = new SelectList(students);
    return View();
}

You might want to fill the dropdown with values from Database, so you should inform the Value and Text columns:

ViewBag.DropDownStudent = new SelectList(yourList, "student_id", "student_name");

I hope it helps.

Fabio Belz
  • 258
  • 2
  • 6
  • 12
  • This was very helpful but, i get Exception that no ViewData item has the key 'DropDownStudent' ? Why so? – FarCry88 Sep 01 '15 at 14:44
  • You got the exception because the ViewData was empty or filled with a different key. Make sure if the ViewData is been properly filled in the action of the Controller using the same key you are calling in the view. For example, if you are using Html.DropDownList("DropDownStudent", "") in the view, you should pass the value using ViewBag.DropDownStudent = new SelectList(yourlist). In this case "DropDownStudent" is the key. By the way, you can use either "ViewBag.DropDownStudent = new SelectList(yourlist)" or "ViewData["DropDownStudent"] = new SelectList(yourlist)". – Fabio Belz Sep 01 '15 at 15:49