@@ -19,6 +19,7 @@ public class SelectExtensionsTest : IDisposable
19
19
{
20
20
private static readonly ViewDataDictionary < FooModel > _listBoxViewData = new ViewDataDictionary < FooModel > { { "foo" , new [ ] { "Bravo" } } } ;
21
21
private static readonly ViewDataDictionary < FooModel > _dropDownListViewData = new ViewDataDictionary < FooModel > { { "foo" , "Bravo" } } ;
22
+ private static readonly ViewDataDictionary < FooContainerModel > _nestedDropDownListViewData = new ViewDataDictionary < FooContainerModel > { { "foo" , "Bravo" } } ;
22
23
private static readonly ViewDataDictionary < NonIEnumerableModel > _nonIEnumerableViewData = new ViewDataDictionary < NonIEnumerableModel > { { "foo" , 1 } } ;
23
24
private static readonly ViewDataDictionary < EnumModel > _enumDropDownListViewData = new ViewDataDictionary < EnumModel >
24
25
{
@@ -1191,6 +1192,48 @@ public void DropDownListForUsesLambdaDefaultValue()
1191
1192
+ "</select>" ,
1192
1193
html . ToHtmlString ( ) ) ;
1193
1194
}
1195
+
1196
+ [ Fact ]
1197
+ public void DropDownListForUsesLambdaDefaultValueWhenNested ( )
1198
+ {
1199
+ // Arrange
1200
+ FooModel model = new FooModel { foo = "Bravo" } ;
1201
+ ViewDataDictionary < FooModel > viewData = new ViewDataDictionary < FooModel > ( model ) ;
1202
+ ViewDataDictionary < string > nestedViewData = MvcHelper . GetNestedViewData ( viewData , m => m . foo ) ;
1203
+ HtmlHelper < string > helper = MvcHelper . GetHtmlHelper ( nestedViewData ) ;
1204
+ SelectList selectList = new SelectList ( MultiSelectListTest . GetSampleStrings ( ) ) ;
1205
+
1206
+ // Act
1207
+ MvcHtmlString html = helper . DropDownListFor ( m => m , selectList ) ;
1208
+
1209
+ // Assert
1210
+ Assert . Equal (
1211
+ "<select id=\" foo\" name=\" foo\" ><option>Alpha</option>" + Environment . NewLine
1212
+ + "<option selected=\" selected\" >Bravo</option>" + Environment . NewLine
1213
+ + "<option>Charlie</option>" + Environment . NewLine
1214
+ + "</select>" ,
1215
+ html . ToHtmlString ( ) ) ;
1216
+ }
1217
+
1218
+ [ Fact ]
1219
+ public void DropDownListForUsesLambdaDefaultValueFromViewDataWhenNested ( )
1220
+ {
1221
+ // Arrange
1222
+ ViewDataDictionary < FooModel > nestedViewData = MvcHelper . GetNestedViewData ( _nestedDropDownListViewData , m => m . inner ) ;
1223
+ HtmlHelper < FooModel > helper = MvcHelper . GetHtmlHelper ( nestedViewData ) ;
1224
+ SelectList selectList = new SelectList ( MultiSelectListTest . GetSampleStrings ( ) ) ;
1225
+
1226
+ // Act
1227
+ MvcHtmlString html = helper . DropDownListFor ( m => m . foo , selectList ) ;
1228
+
1229
+ // Assert
1230
+ Assert . Equal (
1231
+ "<select id=\" inner_foo\" name=\" inner.foo\" ><option>Alpha</option>" + Environment . NewLine
1232
+ + "<option selected=\" selected\" >Bravo</option>" + Environment . NewLine
1233
+ + "<option>Charlie</option>" + Environment . NewLine
1234
+ + "</select>" ,
1235
+ html . ToHtmlString ( ) ) ;
1236
+ }
1194
1237
1195
1238
[ Fact ]
1196
1239
public void DropDownListForUsesLambdaDefaultValueWithNullSelectListUsesViewData ( )
@@ -1215,6 +1258,33 @@ public void DropDownListForUsesLambdaDefaultValueWithNullSelectListUsesViewData(
1215
1258
html . ToHtmlString ( ) ) ;
1216
1259
}
1217
1260
1261
+ [ Fact ]
1262
+ public void DropDownListForUsesLambdaDefaultValueWithNullSelectListUsesViewDataWhenNested ( )
1263
+ {
1264
+ // Arrange
1265
+ FooContainerModel model = new FooContainerModel { inner = new FooModel { foo = "Bravo" } } ;
1266
+ ViewDataDictionary < FooContainerModel > vdd = new ViewDataDictionary < FooContainerModel > ( model )
1267
+ {
1268
+ { "foo" , new SelectList ( MultiSelectListTest . GetSampleStrings ( ) ) }
1269
+ } ;
1270
+
1271
+ ViewDataDictionary < FooModel > nestedViewData = MvcHelper . GetNestedViewData ( vdd , m => m . inner ) ;
1272
+
1273
+
1274
+ HtmlHelper < FooModel > helper = MvcHelper . GetHtmlHelper ( nestedViewData ) ;
1275
+
1276
+ // Act
1277
+ MvcHtmlString html = helper . DropDownListFor ( m => m . foo , selectList : null ) ;
1278
+
1279
+ // Assert
1280
+ Assert . Equal (
1281
+ "<select id=\" inner_foo\" name=\" inner.foo\" ><option>Alpha</option>" + Environment . NewLine
1282
+ + "<option selected=\" selected\" >Bravo</option>" + Environment . NewLine
1283
+ + "<option>Charlie</option>" + Environment . NewLine
1284
+ + "</select>" ,
1285
+ html . ToHtmlString ( ) ) ;
1286
+ }
1287
+
1218
1288
[ Fact ]
1219
1289
public void DropDownListForWithAttributesDictionary ( )
1220
1290
{
@@ -3330,6 +3400,10 @@ private static IEnumerable<SelectListItem> GetSelectListWithNumericValuesForEnum
3330
3400
return selectList ;
3331
3401
}
3332
3402
3403
+ private class FooContainerModel
3404
+ {
3405
+ public FooModel inner { get ; set ; }
3406
+ }
3333
3407
private class FooModel
3334
3408
{
3335
3409
public string foo { get ; set ; }
0 commit comments