Retrieving Data from Html.DropDownList () in a Controller (ASP MVC) | line returned?

I have the following problem:

I have a form in site / banen (currently a local web server) that uses an SQL database. The link is created using ADO.net and is created in the controller as follows:

DBModelEntities _entities;
_entities = new DBModelEntities(); // this part is in the constructor of the controller.

Then I use this database to populate the Html.DropDownList () in my opinion. This is done in two steps. On the controller side, we have in the constructor:

ViewData["EducationLevels"] = this.GetAllEducationLevels();

and helper method:

public SelectList GetAllEducationLevels()
{
     List<EducationLevels> lstEducationLevels = _entities.EducationLevels.ToList();
     SelectList slist = new SelectList(lstEducationLevels, "ID", "Name");
     return slist;
}

In the view, I have the following:

<% using (Html.BeginForm()) {%>

    <fieldset>
        <legend>Fields</legend>

        <!-- various textfields here -->

        <p>
            <label for="EducationLevels">EducationLevels:</label>
            <!-- <%= Html.DropDownList("EducationLevels", ViewData["EducationLevels"] as SelectList)%> -->
            <%= Html.DropDownList("EducationLevels", "..select option..")%>
        </p>
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>

<% } %>

, . .. , . . :

//
    // POST: /Banen/Create

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Create(FormCollection form)
    {
        // set rest of information which has to be set automatically
        var vacatureToAdd = new Vacatures();
        //vacatureToAdd.EducationLevels = form["EducationLevels"];

        // Deserialize (Include white list!)
        TryUpdateModel(vacatureToAdd);

        // Validate
        if (String.IsNullOrEmpty(vacatureToAdd.Title))
            ModelState.AddModelError("Title", "Title is required!");
        if (String.IsNullOrEmpty(vacatureToAdd.Content))
            ModelState.AddModelError("Content", "Content is required!");

        // Update the variables not set in the form
        vacatureToAdd.CreatedAt = DateTime.Now;                 // Just created.
        vacatureToAdd.UpdatedAt = DateTime.Now;                 // Just created, so also modified now.
        vacatureToAdd.ViewCount = 0;                            // We have just created it, so no views
        vacatureToAdd.ID = GetGuid();                           // Generate uniqueidentifier

        try
        {
            // TODO: Add insert logic here
            _entities.AddToVacatures(vacatureToAdd);
            _entities.SaveChanges();

            // Return to listing page if succesful
            return RedirectToAction("Index");
        }
        catch (Exception e)
        {
            return View();
        }
    } 
    #endregion

:

alt text http://www.bastijn.nl/zooi/error_dropdown.png

, , , :

vacatureToAdd.EducationLevels = form["EducationLevels"];

. ASP.net, , , , . , selectedValue , SelectList, , . , .

Sidenote: , .

.

+1
1

<SELECT> , Html.DropDownList(), . EducationLevels . .

+1

All Articles