Model binding form to a List using dropdown. binding failing 1 way for dropdown
I have an action called Sessies. In this action i am creating 'Sessies' objects from a form. if they don't exist i add them in the DB, if there are already Sessies objects connected to the 'Reeksen' object, i load the 'Sessies' into the form so that they can be edited. so i have a create and edit in 1 and the same form.
Also, a 'Reeksen' has a predefined number of 'Sessies' which can not be changed. so i let the user make all 'Sessies' in one time (cos the amount of sessies will be from 1 to 10)
<%@ Page Title="" Language="C#" MasterPageFile="开发者_StackOverflow~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<List<MVC2_NASTEST.Models.FlatSessie>>" %>
...
<h2>
Sessies</h2>
<% using (Html.BeginForm()) {%>
<%= Html.ValidationSummary(true) %>
<fieldset>
<legend>Fields</legend>
<% for (int i = 0; i < Model.Count; i++) { %>
<%= Html.HiddenFor(model => model[i].Ses_ID)%>
<%= Html.HiddenFor(model => model[i].Ses_Rks_ID)%>
<div class="editor-label">
<%= Html.LabelFor(model => model[i].Ses_Nummer)%>
</div>
<div class="editor-field">
<%= Html.HiddenFor(model => model[i].Ses_Nummer)%>
<%= Html.Label(Model[i].Ses_Nummer.ToString())%>
</div>
....
<div class="editor-label">
<%= Html.LabelFor(model => model[i].Ses_LG_ID)%>
</div>
<div class="editor-field">
<%= Html.DropDownListFor(model => model[i].Ses_LG_ID, MVC2_NASTEST.MvcApplication.lesgeverList(), "Selecteer een lesgever...")%>
<%= Html.ValidationMessageFor(model => model[i].Ses_LG_ID)%>
</div>
<div class="editor-label">
<%= Html.LabelFor(model => model[i].Ses_Lpl_ID)%>
</div>
<div class="editor-field">
<%= Html.DropDownListFor(model => model[i].Ses_Lpl_ID, (ViewData["lesplist"] as List<List<SelectListItem>>)[i], "Selecteer een lesplaats...")%>
<%= Html.ValidationMessageFor(model => model[i].Ses_Lpl_ID)%>
</div>
<% } %>
<p>
<input type="submit" value="Create" />
</p>
in my aspx i use a for loop which goes over the List (a FlatSessie is a Sessie flattened strings.)
namespace MVC2_NASTEST.Models {
public partial class FlatSessie {
public int Ses_ID { get; set; }
public int Ses_Nummer { get; set; }
public string Ses_Datum { get; set; }
public string Ses_Beginuur { get; set; }
public string Ses_Einduur { get; set; }
public int Ses_Lpl_ID { get; set; }
public int Ses_Rks_ID { get; set; }
public int Ses_LG_ID { get; set; }
}
}
so, in my code it goes like this:
int antses = m.Mod_AantalSessies.Value;
List<List<SelectListItem>> lpllst = new List<List<SelectListItem>>(antses);
List<FlatSessie> sl = new List<FlatSessie>(antses);
Reeksen rks = _db.Reeksens.First(r => r.Rks_ID == id)
...
List<Sessies> sesl = rks.Sessies.ToList();
for (int i = 0; i < antses; i++) {
sl.Add(Mapper.Map<Sessies, FlatSessie>(sesl[i]));
lpllst.Add(MvcApplication.lesplaatsList(schooljaarparam, sesl[i].Ses_Lpl_ID));
}
...
ViewData["lesplist"] = lpllst;
ViewData["lglist"] = MvcApplication.lesgeverList();
return View(sl);
and the lesgeverlist() method
public static List<SelectListItem> lesgeverList() {
NASDataContext _db = new NASDataContext();
var lesg = (from l in _db.Lesgevers
where l.LG_Naam != "leeg"
orderby l.LG_Naam
select l).ToSelectList(m => m.LG_Naam + " " + m.LG_Vnaam, m => m.LG_ID.ToString(), m => m.LG_ID < -1);
return lesg.ToList();
}
now the problem:
this all works brilliantly. the List goes to the ASPX, i get the form as much times as there are items in the List, and postback works also, the parsing goes and everything. so all is good except for 1 point: the dropdowns.
usually in MVC i don't set any selected value for a SelectList or for a List because they dont need it, in the Edit page, MVC sets those selected items itself on binding.
now however, with the form in the Foreach loop, all fields get filled besides the dropdown boxes, these do not receive their 'initial value'.
however when i set an item in the List as selected, it does get selected in the form. (as seen from the ViewData["lesplist"]) but when i send a normal List with no selected value, the model binder does not propagate it's given value for that field to the selectedvalue of the dropdown.
however, when i do a form submit, and i return the view (because of validation failed) the dropdowns DO keep their value.
Is this fixable, or is this just a flaw in MVC2?
DropDownListFor not binding on Edit View with repeating items (List<T>)
there is the answer :)
精彩评论