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>
<p>
<label for="EducationLevels">EducationLevels:</label>
<%= Html.DropDownList("EducationLevels", "..select option..")%>
</p>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
<% } %>
, . .. , . . :
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(FormCollection form)
{
var vacatureToAdd = new Vacatures();
TryUpdateModel(vacatureToAdd);
if (String.IsNullOrEmpty(vacatureToAdd.Title))
ModelState.AddModelError("Title", "Title is required!");
if (String.IsNullOrEmpty(vacatureToAdd.Content))
ModelState.AddModelError("Content", "Content is required!");
vacatureToAdd.CreatedAt = DateTime.Now;
vacatureToAdd.UpdatedAt = DateTime.Now;
vacatureToAdd.ViewCount = 0;
vacatureToAdd.ID = GetGuid();
try
{
_entities.AddToVacatures(vacatureToAdd);
_entities.SaveChanges();
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: , .
.