PageRenderTime 97ms CodeModel.GetById 19ms RepoModel.GetById 2ms app.codeStats 0ms

/test/System.Web.Mvc.Test/Html/Test/SelectExtensionsTest.cs

https://bitbucket.org/mdavid/aspnetwebstack
C# | 1300 lines | 896 code | 203 blank | 201 comment | 0 complexity | 0dbf27e6afe6c00b5e6a18917b5c51b9 MD5 | raw file
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Web.Mvc.Test;
  7. using Microsoft.Web.UnitTestUtil;
  8. using Xunit;
  9. using Assert = Microsoft.TestCommon.AssertEx;
  10. namespace System.Web.Mvc.Html.Test
  11. {
  12. public class SelectExtensionsTest
  13. {
  14. private static readonly ViewDataDictionary<FooModel> _listBoxViewData = new ViewDataDictionary<FooModel> { { "foo", new[] { "Bravo" } } };
  15. private static readonly ViewDataDictionary<FooModel> _dropDownListViewData = new ViewDataDictionary<FooModel> { { "foo", "Bravo" } };
  16. private static readonly ViewDataDictionary<NonIEnumerableModel> _nonIEnumerableViewData = new ViewDataDictionary<NonIEnumerableModel> { { "foo", 1 } };
  17. private static ViewDataDictionary GetViewDataWithSelectList()
  18. {
  19. ViewDataDictionary viewData = new ViewDataDictionary();
  20. SelectList selectList = new SelectList(MultiSelectListTest.GetSampleAnonymousObjects(), "Letter", "FullWord", "C");
  21. viewData["foo"] = selectList;
  22. viewData["foo.bar"] = selectList;
  23. return viewData;
  24. }
  25. // DropDownList
  26. [Fact]
  27. public void DropDownListUsesExplicitValueIfNotProvidedInViewData()
  28. {
  29. // Arrange
  30. HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary());
  31. SelectList selectList = new SelectList(MultiSelectListTest.GetSampleAnonymousObjects(), "Letter", "FullWord", "C");
  32. // Act
  33. MvcHtmlString html = helper.DropDownList("foo", selectList, (string)null /* optionLabel */);
  34. // Assert
  35. Assert.Equal(
  36. @"<select id=""foo"" name=""foo""><option value=""A"">Alpha</option>
  37. <option value=""B"">Bravo</option>
  38. <option selected=""selected"" value=""C"">Charlie</option>
  39. </select>",
  40. html.ToHtmlString());
  41. }
  42. [Fact]
  43. public void DropDownListUsesExplicitValueIfNotProvidedInViewData_Unobtrusive()
  44. {
  45. // Arrange
  46. HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary());
  47. helper.ViewContext.ClientValidationEnabled = true;
  48. helper.ViewContext.UnobtrusiveJavaScriptEnabled = true;
  49. helper.ViewContext.FormContext = new FormContext();
  50. helper.ClientValidationRuleFactory = (name, metadata) => new[] { new ModelClientValidationRule { ValidationType = "type", ErrorMessage = "error" } };
  51. SelectList selectList = new SelectList(MultiSelectListTest.GetSampleAnonymousObjects(), "Letter", "FullWord", "C");
  52. // Act
  53. MvcHtmlString html = helper.DropDownList("foo", selectList, (string)null /* optionLabel */);
  54. // Assert
  55. Assert.Equal(
  56. @"<select data-val=""true"" data-val-type=""error"" id=""foo"" name=""foo""><option value=""A"">Alpha</option>
  57. <option value=""B"">Bravo</option>
  58. <option selected=""selected"" value=""C"">Charlie</option>
  59. </select>",
  60. html.ToHtmlString());
  61. }
  62. [Fact]
  63. public void DropDownListUsesViewDataDefaultValue()
  64. {
  65. // Arrange
  66. HtmlHelper helper = MvcHelper.GetHtmlHelper(_dropDownListViewData);
  67. SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings(), "Charlie");
  68. // Act
  69. MvcHtmlString html = helper.DropDownList("foo", selectList, (string)null /* optionLabel */);
  70. // Assert
  71. Assert.Equal(
  72. @"<select id=""foo"" name=""foo""><option>Alpha</option>
  73. <option selected=""selected"">Bravo</option>
  74. <option>Charlie</option>
  75. </select>",
  76. html.ToHtmlString());
  77. }
  78. [Fact]
  79. public void DropDownListUsesViewDataDefaultValueNoOptionLabel()
  80. {
  81. // Arrange
  82. HtmlHelper helper = MvcHelper.GetHtmlHelper(_dropDownListViewData);
  83. SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings(), "Charlie");
  84. // Act
  85. MvcHtmlString html = helper.DropDownList("foo", selectList);
  86. // Assert
  87. Assert.Equal(
  88. @"<select id=""foo"" name=""foo""><option>Alpha</option>
  89. <option selected=""selected"">Bravo</option>
  90. <option>Charlie</option>
  91. </select>",
  92. html.ToHtmlString());
  93. }
  94. [Fact]
  95. public void DropDownListWithAttributesDictionary()
  96. {
  97. // Arrange
  98. HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary());
  99. SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings());
  100. // Act
  101. MvcHtmlString html = helper.DropDownList("foo", selectList, null /* optionLabel */, HtmlHelperTest.AttributesDictionary);
  102. // Assert
  103. Assert.Equal(
  104. @"<select baz=""BazValue"" id=""foo"" name=""foo""><option>Alpha</option>
  105. <option>Bravo</option>
  106. <option>Charlie</option>
  107. </select>",
  108. html.ToHtmlString());
  109. }
  110. [Fact]
  111. public void DropDownListWithEmptyNameThrows()
  112. {
  113. // Arrange
  114. HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary());
  115. // Act & Assert
  116. Assert.ThrowsArgumentNullOrEmpty(
  117. delegate { helper.DropDownList(String.Empty, (SelectList)null /* selectList */, (string)null /* optionLabel */); },
  118. "name");
  119. }
  120. [Fact]
  121. public void DropDownListWithErrors()
  122. {
  123. // Arrange
  124. SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings(), new[] { "Charlie" });
  125. ViewDataDictionary viewData = GetViewDataWithErrors();
  126. HtmlHelper helper = MvcHelper.GetHtmlHelper(viewData);
  127. // Act
  128. MvcHtmlString html = helper.DropDownList("foo", selectList, null /* optionLabel */, HtmlHelperTest.AttributesObjectDictionary);
  129. // Assert
  130. Assert.Equal(
  131. @"<select baz=""BazObjValue"" class=""input-validation-error"" id=""foo"" name=""foo""><option>Alpha</option>
  132. <option selected=""selected"">Bravo</option>
  133. <option>Charlie</option>
  134. </select>",
  135. html.ToHtmlString());
  136. }
  137. [Fact]
  138. public void DropDownListWithErrorsAndCustomClass()
  139. {
  140. // Arrange
  141. SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings());
  142. ViewDataDictionary viewData = GetViewDataWithErrors();
  143. HtmlHelper helper = MvcHelper.GetHtmlHelper(viewData);
  144. // Act
  145. MvcHtmlString html = helper.DropDownList("foo", selectList, null /* optionLabel */, new { @class = "foo-class" });
  146. // Assert
  147. Assert.Equal(
  148. @"<select class=""input-validation-error foo-class"" id=""foo"" name=""foo""><option>Alpha</option>
  149. <option selected=""selected"">Bravo</option>
  150. <option>Charlie</option>
  151. </select>",
  152. html.ToHtmlString());
  153. }
  154. [Fact]
  155. public void DropDownListWithNullNameThrows()
  156. {
  157. // Arrange
  158. HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary());
  159. // Act & Assert
  160. Assert.ThrowsArgumentNullOrEmpty(
  161. delegate { helper.DropDownList(null /* name */, (SelectList)null /* selectList */, (string)null /* optionLabel */); },
  162. "name");
  163. }
  164. [Fact]
  165. public void DropDownListWithNullSelectListUsesViewData()
  166. {
  167. // Arrange
  168. HtmlHelper helper = MvcHelper.GetHtmlHelper();
  169. helper.ViewData["foo"] = new MultiSelectList(MultiSelectListTest.GetSampleStrings(), new[] { "Charlie" });
  170. // Act
  171. MvcHtmlString html = helper.DropDownList("foo");
  172. // Assert
  173. Assert.Equal(
  174. @"<select id=""foo"" name=""foo""><option>Alpha</option>
  175. <option>Bravo</option>
  176. <option selected=""selected"">Charlie</option>
  177. </select>",
  178. html.ToHtmlString());
  179. }
  180. [Fact]
  181. public void DropDownListWithObjectDictionary()
  182. {
  183. // Arrange
  184. SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings());
  185. ViewDataDictionary viewData = new ViewDataDictionary();
  186. HtmlHelper helper = MvcHelper.GetHtmlHelper(viewData);
  187. // Act
  188. MvcHtmlString html = helper.DropDownList("foo", selectList, null /* optionLabel */, HtmlHelperTest.AttributesObjectDictionary);
  189. // Assert
  190. Assert.Equal(
  191. @"<select baz=""BazObjValue"" id=""foo"" name=""foo""><option>Alpha</option>
  192. <option>Bravo</option>
  193. <option>Charlie</option>
  194. </select>",
  195. html.ToHtmlString());
  196. }
  197. [Fact]
  198. public void DropDownListWithObjectDictionaryWithUnderscores()
  199. {
  200. // Arrange
  201. SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings());
  202. ViewDataDictionary viewData = new ViewDataDictionary();
  203. HtmlHelper helper = MvcHelper.GetHtmlHelper(viewData);
  204. // Act
  205. MvcHtmlString html = helper.DropDownList("foo", selectList, null /* optionLabel */, HtmlHelperTest.AttributesObjectUnderscoresDictionary);
  206. // Assert
  207. Assert.Equal(
  208. @"<select foo-baz=""BazObjValue"" id=""foo"" name=""foo""><option>Alpha</option>
  209. <option>Bravo</option>
  210. <option>Charlie</option>
  211. </select>",
  212. html.ToHtmlString());
  213. }
  214. [Fact]
  215. public void DropDownListWithObjectDictionaryAndSelectList()
  216. {
  217. // Arrange
  218. HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary());
  219. SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings());
  220. // Act
  221. MvcHtmlString html = helper.DropDownList("foo", selectList, null /* optionLabel */, HtmlHelperTest.AttributesObjectDictionary);
  222. // Assert
  223. Assert.Equal(
  224. @"<select baz=""BazObjValue"" id=""foo"" name=""foo""><option>Alpha</option>
  225. <option>Bravo</option>
  226. <option>Charlie</option>
  227. </select>",
  228. html.ToHtmlString());
  229. }
  230. [Fact]
  231. public void DropDownListWithObjectDictionaryAndSelectListNoOptionLabel()
  232. {
  233. // Arrange
  234. HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary());
  235. SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings());
  236. // Act
  237. MvcHtmlString html = helper.DropDownList("foo", selectList, HtmlHelperTest.AttributesObjectDictionary);
  238. // Assert
  239. Assert.Equal(
  240. @"<select baz=""BazObjValue"" id=""foo"" name=""foo""><option>Alpha</option>
  241. <option>Bravo</option>
  242. <option>Charlie</option>
  243. </select>",
  244. html.ToHtmlString());
  245. }
  246. [Fact]
  247. public void DropDownListWithObjectDictionaryWithUnderscoresAndSelectListNoOptionLabel()
  248. {
  249. // Arrange
  250. HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary());
  251. SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings());
  252. // Act
  253. MvcHtmlString html = helper.DropDownList("foo", selectList, HtmlHelperTest.AttributesObjectUnderscoresDictionary);
  254. // Assert
  255. Assert.Equal(
  256. @"<select foo-baz=""BazObjValue"" id=""foo"" name=""foo""><option>Alpha</option>
  257. <option>Bravo</option>
  258. <option>Charlie</option>
  259. </select>",
  260. html.ToHtmlString());
  261. }
  262. [Fact]
  263. public void DropDownListWithObjectDictionaryAndEmptyOptionLabel()
  264. {
  265. // Arrange
  266. HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary());
  267. SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings());
  268. // Act
  269. MvcHtmlString html = helper.DropDownList("foo", selectList, String.Empty /* optionLabel */, HtmlHelperTest.AttributesObjectDictionary);
  270. // Assert
  271. Assert.Equal(
  272. @"<select baz=""BazObjValue"" id=""foo"" name=""foo""><option value=""""></option>
  273. <option>Alpha</option>
  274. <option>Bravo</option>
  275. <option>Charlie</option>
  276. </select>",
  277. html.ToHtmlString());
  278. }
  279. [Fact]
  280. public void DropDownListWithObjectDictionaryAndTitle()
  281. {
  282. // Arrange
  283. HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary());
  284. SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings());
  285. // Act
  286. MvcHtmlString html = helper.DropDownList("foo", selectList, "[Select Something]", HtmlHelperTest.AttributesObjectDictionary);
  287. // Assert
  288. Assert.Equal(
  289. @"<select baz=""BazObjValue"" id=""foo"" name=""foo""><option value="""">[Select Something]</option>
  290. <option>Alpha</option>
  291. <option>Bravo</option>
  292. <option>Charlie</option>
  293. </select>",
  294. html.ToHtmlString());
  295. }
  296. [Fact]
  297. public void DropDownListUsesViewDataSelectList()
  298. {
  299. // Arrange
  300. HtmlHelper helper = MvcHelper.GetHtmlHelper(GetViewDataWithSelectList());
  301. // Act
  302. MvcHtmlString html = helper.DropDownList("foo", (string)null /* optionLabel */);
  303. // Assert
  304. Assert.Equal(
  305. @"<select id=""foo"" name=""foo""><option value=""A"">Alpha</option>
  306. <option value=""B"">Bravo</option>
  307. <option selected=""selected"" value=""C"">Charlie</option>
  308. </select>",
  309. html.ToHtmlString());
  310. }
  311. [Fact]
  312. public void DropDownListUsesModelState()
  313. {
  314. // Arrange
  315. SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings());
  316. ViewDataDictionary viewData = GetViewDataWithErrors();
  317. viewData["foo"] = selectList;
  318. HtmlHelper helper = MvcHelper.GetHtmlHelper(viewData);
  319. // Act
  320. MvcHtmlString html = helper.DropDownList("foo");
  321. // Assert
  322. Assert.Equal(
  323. @"<select class=""input-validation-error"" id=""foo"" name=""foo""><option>Alpha</option>
  324. <option selected=""selected"">Bravo</option>
  325. <option>Charlie</option>
  326. </select>",
  327. html.ToHtmlString());
  328. }
  329. [Fact]
  330. public void DropDownListUsesViewDataSelectListNoOptionLabel()
  331. {
  332. // Arrange
  333. HtmlHelper helper = MvcHelper.GetHtmlHelper(GetViewDataWithSelectList());
  334. // Act
  335. MvcHtmlString html = helper.DropDownList("foo");
  336. // Assert
  337. Assert.Equal(
  338. @"<select id=""foo"" name=""foo""><option value=""A"">Alpha</option>
  339. <option value=""B"">Bravo</option>
  340. <option selected=""selected"" value=""C"">Charlie</option>
  341. </select>",
  342. html.ToHtmlString());
  343. }
  344. [Fact]
  345. public void DropDownListWithDotReplacementForId()
  346. {
  347. // Arrange
  348. HtmlHelper helper = MvcHelper.GetHtmlHelper(GetViewDataWithSelectList());
  349. // Act
  350. MvcHtmlString html = helper.DropDownList("foo.bar");
  351. // Assert
  352. Assert.Equal(
  353. @"<select id=""foo_bar"" name=""foo.bar""><option value=""A"">Alpha</option>
  354. <option value=""B"">Bravo</option>
  355. <option selected=""selected"" value=""C"">Charlie</option>
  356. </select>",
  357. html.ToHtmlString());
  358. }
  359. [Fact]
  360. public void DropDownListWithIEnumerableSelectListItem()
  361. {
  362. // Arrange
  363. ViewDataDictionary vdd = new ViewDataDictionary { { "foo", MultiSelectListTest.GetSampleIEnumerableObjects() } };
  364. HtmlHelper helper = MvcHelper.GetHtmlHelper(vdd);
  365. // Act
  366. MvcHtmlString html = helper.DropDownList("foo");
  367. // Assert
  368. Assert.Equal(
  369. @"<select id=""foo"" name=""foo""><option value=""123456789"">John</option>
  370. <option value=""987654321"">Jane</option>
  371. <option selected=""selected"" value=""111111111"">Joe</option>
  372. </select>",
  373. html.ToHtmlString());
  374. }
  375. [Fact]
  376. public void DropDownListWithIEnumerableSelectListItemSelectsDefaultFromViewData()
  377. {
  378. // Arrange
  379. ViewDataDictionary vdd = new ViewDataDictionary { { "foo", "123456789" } };
  380. HtmlHelper helper = MvcHelper.GetHtmlHelper(vdd);
  381. // Act
  382. MvcHtmlString html = helper.DropDownList("foo", MultiSelectListTest.GetSampleIEnumerableObjects());
  383. // Assert
  384. Assert.Equal(
  385. @"<select id=""foo"" name=""foo""><option selected=""selected"" value=""123456789"">John</option>
  386. <option value=""987654321"">Jane</option>
  387. <option value=""111111111"">Joe</option>
  388. </select>",
  389. html.ToHtmlString());
  390. }
  391. [Fact]
  392. public void DropDownListWithListOfSelectListItemSelectsDefaultFromViewData()
  393. {
  394. // Arrange
  395. ViewDataDictionary vdd = new ViewDataDictionary { { "foo", "123456789" } };
  396. HtmlHelper helper = MvcHelper.GetHtmlHelper(vdd);
  397. // Act
  398. MvcHtmlString html = helper.DropDownList("foo", MultiSelectListTest.GetSampleListObjects());
  399. // Assert
  400. Assert.Equal(
  401. @"<select id=""foo"" name=""foo""><option selected=""selected"" value=""123456789"">John</option>
  402. <option value=""987654321"">Jane</option>
  403. <option value=""111111111"">Joe</option>
  404. </select>",
  405. html.ToHtmlString());
  406. }
  407. [Fact]
  408. public void DropDownListWithListOfSelectListItem()
  409. {
  410. // Arrange
  411. ViewDataDictionary vdd = new ViewDataDictionary { { "foo", MultiSelectListTest.GetSampleListObjects() } };
  412. HtmlHelper helper = MvcHelper.GetHtmlHelper(vdd);
  413. // Act
  414. MvcHtmlString html = helper.DropDownList("foo");
  415. // Assert
  416. Assert.Equal(
  417. @"<select id=""foo"" name=""foo""><option value=""123456789"">John</option>
  418. <option value=""987654321"">Jane</option>
  419. <option selected=""selected"" value=""111111111"">Joe</option>
  420. </select>",
  421. html.ToHtmlString());
  422. }
  423. [Fact]
  424. public void DropDownListWithNullViewDataValueThrows()
  425. {
  426. // Arrange
  427. HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary());
  428. // Act
  429. Assert.Throws<InvalidOperationException>(
  430. delegate { helper.DropDownList("foo", (string)null /* optionLabel */); },
  431. "There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'foo'.");
  432. }
  433. [Fact]
  434. public void DropDownListWithWrongViewDataTypeValueThrows()
  435. {
  436. // Arrange
  437. HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary { { "foo", 123 } });
  438. // Act
  439. Assert.Throws<InvalidOperationException>(
  440. delegate { helper.DropDownList("foo", (string)null /* optionLabel */); },
  441. "The ViewData item that has the key 'foo' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'.");
  442. }
  443. [Fact]
  444. public void DropDownListWithPrefix()
  445. {
  446. // Arrange
  447. HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary());
  448. SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings());
  449. helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix";
  450. // Act
  451. MvcHtmlString html = helper.DropDownList("foo", selectList, HtmlHelperTest.AttributesObjectDictionary);
  452. // Assert
  453. Assert.Equal(
  454. @"<select baz=""BazObjValue"" id=""MyPrefix_foo"" name=""MyPrefix.foo""><option>Alpha</option>
  455. <option>Bravo</option>
  456. <option>Charlie</option>
  457. </select>",
  458. html.ToHtmlString());
  459. }
  460. [Fact]
  461. public void DropDownListWithPrefixAndEmptyName()
  462. {
  463. // Arrange
  464. HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary());
  465. SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings());
  466. helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix";
  467. // Act
  468. MvcHtmlString html = helper.DropDownList("", selectList, HtmlHelperTest.AttributesObjectDictionary);
  469. // Assert
  470. Assert.Equal(
  471. @"<select baz=""BazObjValue"" id=""MyPrefix"" name=""MyPrefix""><option>Alpha</option>
  472. <option>Bravo</option>
  473. <option>Charlie</option>
  474. </select>",
  475. html.ToHtmlString());
  476. }
  477. [Fact]
  478. public void DropDownListWithPrefixAndNullSelectListUsesViewData()
  479. {
  480. // Arrange
  481. HtmlHelper helper = MvcHelper.GetHtmlHelper();
  482. helper.ViewData["foo"] = new MultiSelectList(MultiSelectListTest.GetSampleStrings(), new[] { "Charlie" });
  483. helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix";
  484. // Act
  485. MvcHtmlString html = helper.DropDownList("foo");
  486. // Assert
  487. Assert.Equal(
  488. @"<select id=""MyPrefix_foo"" name=""MyPrefix.foo""><option>Alpha</option>
  489. <option>Bravo</option>
  490. <option selected=""selected"">Charlie</option>
  491. </select>",
  492. html.ToHtmlString());
  493. }
  494. // DropDownListFor
  495. [Fact]
  496. public void DropDownListForWithNullExpressionThrows()
  497. {
  498. // Arrange
  499. HtmlHelper<object> helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary<object>());
  500. SelectList selectList = new SelectList(MultiSelectListTest.GetSampleAnonymousObjects(), "Letter", "FullWord", "C");
  501. // Act & Assert
  502. Assert.ThrowsArgumentNull(
  503. () => helper.DropDownListFor<object, object>(null /* expression */, selectList),
  504. "expression"
  505. );
  506. }
  507. [Fact]
  508. public void DropDownListForUsesExplicitValueIfNotProvidedInViewData()
  509. {
  510. // Arrange
  511. HtmlHelper<FooModel> helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary<FooModel>());
  512. SelectList selectList = new SelectList(MultiSelectListTest.GetSampleAnonymousObjects(), "Letter", "FullWord", "C");
  513. // Act
  514. MvcHtmlString html = helper.DropDownListFor(m => m.foo, selectList, (string)null /* optionLabel */);
  515. // Assert
  516. Assert.Equal(
  517. @"<select id=""foo"" name=""foo""><option value=""A"">Alpha</option>
  518. <option value=""B"">Bravo</option>
  519. <option selected=""selected"" value=""C"">Charlie</option>
  520. </select>",
  521. html.ToHtmlString());
  522. }
  523. [Fact]
  524. public void DropDownListForUsesExplicitValueIfNotProvidedInViewData_Unobtrusive()
  525. {
  526. // Arrange
  527. HtmlHelper<FooModel> helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary<FooModel>());
  528. helper.ViewContext.ClientValidationEnabled = true;
  529. helper.ViewContext.UnobtrusiveJavaScriptEnabled = true;
  530. helper.ViewContext.FormContext = new FormContext();
  531. helper.ClientValidationRuleFactory = (name, metadata) => new[] { new ModelClientValidationRule { ValidationType = "type", ErrorMessage = "error" } };
  532. SelectList selectList = new SelectList(MultiSelectListTest.GetSampleAnonymousObjects(), "Letter", "FullWord", "C");
  533. // Act
  534. MvcHtmlString html = helper.DropDownListFor(m => m.foo, selectList, (string)null /* optionLabel */);
  535. // Assert
  536. Assert.Equal(
  537. @"<select data-val=""true"" data-val-type=""error"" id=""foo"" name=""foo""><option value=""A"">Alpha</option>
  538. <option value=""B"">Bravo</option>
  539. <option selected=""selected"" value=""C"">Charlie</option>
  540. </select>",
  541. html.ToHtmlString());
  542. }
  543. [Fact]
  544. public void DropDownListForWithEnumerableModel_Unobtrusive()
  545. {
  546. // Arrange
  547. HtmlHelper<IEnumerable<RequiredModel>> helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary<IEnumerable<RequiredModel>>());
  548. helper.ViewContext.ClientValidationEnabled = true;
  549. helper.ViewContext.UnobtrusiveJavaScriptEnabled = true;
  550. helper.ViewContext.FormContext = new FormContext();
  551. helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix";
  552. SelectList selectList = new SelectList(MultiSelectListTest.GetSampleAnonymousObjects(), "Letter", "FullWord", "C");
  553. // Act
  554. MvcHtmlString html = helper.DropDownListFor(m => m.ElementAt(0).foo, selectList);
  555. // Assert
  556. Assert.Equal(
  557. @"<select data-val=""true"" data-val-required=""The foo field is required."" id=""MyPrefix_foo"" name=""MyPrefix.foo""><option value=""A"">Alpha</option>
  558. <option value=""B"">Bravo</option>
  559. <option selected=""selected"" value=""C"">Charlie</option>
  560. </select>",
  561. html.ToHtmlString());
  562. }
  563. [Fact]
  564. public void DropDownListForUsesViewDataDefaultValue()
  565. {
  566. // Arrange
  567. HtmlHelper<FooModel> helper = MvcHelper.GetHtmlHelper(_dropDownListViewData);
  568. SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings(), "Charlie");
  569. // Act
  570. MvcHtmlString html = helper.DropDownListFor(m => m.foo, selectList, (string)null /* optionLabel */);
  571. // Assert
  572. Assert.Equal(
  573. @"<select id=""foo"" name=""foo""><option>Alpha</option>
  574. <option selected=""selected"">Bravo</option>
  575. <option>Charlie</option>
  576. </select>",
  577. html.ToHtmlString());
  578. }
  579. [Fact]
  580. public void DropDownListForUsesViewDataDefaultValueNoOptionLabel()
  581. {
  582. // Arrange
  583. HtmlHelper<FooModel> helper = MvcHelper.GetHtmlHelper(_dropDownListViewData);
  584. SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings(), "Charlie");
  585. // Act
  586. MvcHtmlString html = helper.DropDownListFor(m => m.foo, selectList);
  587. // Assert
  588. Assert.Equal(
  589. @"<select id=""foo"" name=""foo""><option>Alpha</option>
  590. <option selected=""selected"">Bravo</option>
  591. <option>Charlie</option>
  592. </select>",
  593. html.ToHtmlString());
  594. }
  595. [Fact]
  596. public void DropDownListForWithAttributesDictionary()
  597. {
  598. // Arrange
  599. HtmlHelper<FooModel> helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary<FooModel>());
  600. SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings());
  601. // Act
  602. MvcHtmlString html = helper.DropDownListFor(m => m.foo, selectList, null /* optionLabel */, HtmlHelperTest.AttributesDictionary);
  603. // Assert
  604. Assert.Equal(
  605. @"<select baz=""BazValue"" id=""foo"" name=""foo""><option>Alpha</option>
  606. <option>Bravo</option>
  607. <option>Charlie</option>
  608. </select>",
  609. html.ToHtmlString());
  610. }
  611. [Fact]
  612. public void DropDownListForWithErrors()
  613. {
  614. // Arrange
  615. HtmlHelper<FooBarModel> helper = MvcHelper.GetHtmlHelper(GetViewDataWithErrors());
  616. SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings(), new[] { "Charlie" });
  617. // Act
  618. MvcHtmlString html = helper.DropDownListFor(m => m.foo, selectList, null /* optionLabel */, HtmlHelperTest.AttributesObjectDictionary);
  619. // Assert
  620. Assert.Equal(
  621. @"<select baz=""BazObjValue"" class=""input-validation-error"" id=""foo"" name=""foo""><option>Alpha</option>
  622. <option selected=""selected"">Bravo</option>
  623. <option>Charlie</option>
  624. </select>",
  625. html.ToHtmlString());
  626. }
  627. [Fact]
  628. public void DropDownListForWithErrorsAndCustomClass()
  629. {
  630. // Arrange
  631. HtmlHelper<FooBarModel> helper = MvcHelper.GetHtmlHelper(GetViewDataWithErrors());
  632. SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings());
  633. // Act
  634. MvcHtmlString html = helper.DropDownListFor(m => m.foo, selectList, null /* optionLabel */, new { @class = "foo-class" });
  635. // Assert
  636. Assert.Equal(
  637. @"<select class=""input-validation-error foo-class"" id=""foo"" name=""foo""><option>Alpha</option>
  638. <option selected=""selected"">Bravo</option>
  639. <option>Charlie</option>
  640. </select>",
  641. html.ToHtmlString());
  642. }
  643. [Fact]
  644. public void DropDownListForWithObjectDictionary()
  645. {
  646. // Arrange
  647. HtmlHelper<FooModel> helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary<FooModel>());
  648. SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings());
  649. // Act
  650. MvcHtmlString html = helper.DropDownListFor(m => m.foo, selectList, null /* optionLabel */, HtmlHelperTest.AttributesObjectDictionary);
  651. // Assert
  652. Assert.Equal(
  653. @"<select baz=""BazObjValue"" id=""foo"" name=""foo""><option>Alpha</option>
  654. <option>Bravo</option>
  655. <option>Charlie</option>
  656. </select>",
  657. html.ToHtmlString());
  658. }
  659. [Fact]
  660. public void DropDownListForWithObjectDictionaryWithUnderscores()
  661. {
  662. // Arrange
  663. HtmlHelper<FooModel> helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary<FooModel>());
  664. SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings());
  665. // Act
  666. MvcHtmlString html = helper.DropDownListFor(m => m.foo, selectList, null /* optionLabel */, HtmlHelperTest.AttributesObjectUnderscoresDictionary);
  667. // Assert
  668. Assert.Equal(
  669. @"<select foo-baz=""BazObjValue"" id=""foo"" name=""foo""><option>Alpha</option>
  670. <option>Bravo</option>
  671. <option>Charlie</option>
  672. </select>",
  673. html.ToHtmlString());
  674. }
  675. [Fact]
  676. public void DropDownListForWithObjectDictionaryAndSelectListNoOptionLabel()
  677. {
  678. // Arrange
  679. HtmlHelper<FooModel> helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary<FooModel>());
  680. SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings());
  681. // Act
  682. MvcHtmlString html = helper.DropDownListFor(m => m.foo, selectList, HtmlHelperTest.AttributesObjectDictionary);
  683. // Assert
  684. Assert.Equal(
  685. @"<select baz=""BazObjValue"" id=""foo"" name=""foo""><option>Alpha</option>
  686. <option>Bravo</option>
  687. <option>Charlie</option>
  688. </select>",
  689. html.ToHtmlString());
  690. }
  691. [Fact]
  692. public void DropDownListForWithObjectDictionaryWithUnderscoresAndSelectListNoOptionLabel()
  693. {
  694. // Arrange
  695. HtmlHelper<FooModel> helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary<FooModel>());
  696. SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings());
  697. // Act
  698. MvcHtmlString html = helper.DropDownListFor(m => m.foo, selectList, HtmlHelperTest.AttributesObjectUnderscoresDictionary);
  699. // Assert
  700. Assert.Equal(
  701. @"<select foo-baz=""BazObjValue"" id=""foo"" name=""foo""><option>Alpha</option>
  702. <option>Bravo</option>
  703. <option>Charlie</option>
  704. </select>",
  705. html.ToHtmlString());
  706. }
  707. [Fact]
  708. public void DropDownListForWithObjectDictionaryAndEmptyOptionLabel()
  709. {
  710. // Arrange
  711. HtmlHelper<FooModel> helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary<FooModel>());
  712. SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings());
  713. // Act
  714. MvcHtmlString html = helper.DropDownListFor(m => m.foo, selectList, String.Empty /* optionLabel */, HtmlHelperTest.AttributesObjectDictionary);
  715. // Assert
  716. Assert.Equal(
  717. @"<select baz=""BazObjValue"" id=""foo"" name=""foo""><option value=""""></option>
  718. <option>Alpha</option>
  719. <option>Bravo</option>
  720. <option>Charlie</option>
  721. </select>",
  722. html.ToHtmlString());
  723. }
  724. [Fact]
  725. public void DropDownListForWithObjectDictionaryAndTitle()
  726. {
  727. // Arrange
  728. HtmlHelper<FooModel> helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary<FooModel>());
  729. SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings());
  730. // Act
  731. MvcHtmlString html = helper.DropDownListFor(m => m.foo, selectList, "[Select Something]", HtmlHelperTest.AttributesObjectDictionary);
  732. // Assert
  733. Assert.Equal(
  734. @"<select baz=""BazObjValue"" id=""foo"" name=""foo""><option value="""">[Select Something]</option>
  735. <option>Alpha</option>
  736. <option>Bravo</option>
  737. <option>Charlie</option>
  738. </select>",
  739. html.ToHtmlString());
  740. }
  741. [Fact]
  742. public void DropDownListForWithIEnumerableSelectListItemSelectsDefaultFromViewData()
  743. {
  744. // Arrange
  745. ViewDataDictionary<FooModel> vdd = new ViewDataDictionary<FooModel> { { "foo", "123456789" } };
  746. HtmlHelper<FooModel> helper = MvcHelper.GetHtmlHelper(vdd);
  747. // Act
  748. MvcHtmlString html = helper.DropDownListFor(m => m.foo, MultiSelectListTest.GetSampleIEnumerableObjects());
  749. // Assert
  750. Assert.Equal(
  751. @"<select id=""foo"" name=""foo""><option selected=""selected"" value=""123456789"">John</option>
  752. <option value=""987654321"">Jane</option>
  753. <option value=""111111111"">Joe</option>
  754. </select>",
  755. html.ToHtmlString());
  756. }
  757. [Fact]
  758. public void DropDownListForWithListOfSelectListItemSelectsDefaultFromViewData()
  759. {
  760. // Arrange
  761. ViewDataDictionary<FooModel> vdd = new ViewDataDictionary<FooModel> { { "foo", "123456789" } };
  762. HtmlHelper<FooModel> helper = MvcHelper.GetHtmlHelper(vdd);
  763. // Act
  764. MvcHtmlString html = helper.DropDownListFor(m => m.foo, MultiSelectListTest.GetSampleListObjects());
  765. // Assert
  766. Assert.Equal(
  767. @"<select id=""foo"" name=""foo""><option selected=""selected"" value=""123456789"">John</option>
  768. <option value=""987654321"">Jane</option>
  769. <option value=""111111111"">Joe</option>
  770. </select>",
  771. html.ToHtmlString());
  772. }
  773. [Fact]
  774. public void DropDownListForWithPrefix()
  775. {
  776. // Arrange
  777. HtmlHelper<FooModel> helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary<FooModel>());
  778. SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings());
  779. helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix";
  780. // Act
  781. MvcHtmlString html = helper.DropDownListFor(m => m.foo, selectList, HtmlHelperTest.AttributesObjectDictionary);
  782. // Assert
  783. Assert.Equal(
  784. @"<select baz=""BazObjValue"" id=""MyPrefix_foo"" name=""MyPrefix.foo""><option>Alpha</option>
  785. <option>Bravo</option>
  786. <option>Charlie</option>
  787. </select>",
  788. html.ToHtmlString());
  789. }
  790. [Fact]
  791. public void DropDownListForWithPrefixAndEmptyName()
  792. {
  793. // Arrange
  794. HtmlHelper<FooModel> helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary<FooModel>());
  795. SelectList selectList = new SelectList(MultiSelectListTest.GetSampleStrings());
  796. helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix";
  797. // Act
  798. MvcHtmlString html = helper.DropDownListFor(m => m, selectList, HtmlHelperTest.AttributesObjectDictionary);
  799. // Assert
  800. Assert.Equal(
  801. @"<select baz=""BazObjValue"" id=""MyPrefix"" name=""MyPrefix""><option>Alpha</option>
  802. <option>Bravo</option>
  803. <option>Charlie</option>
  804. </select>",
  805. html.ToHtmlString());
  806. }
  807. [Fact]
  808. public void DropDownListForWithPrefixAndIEnumerableSelectListItemSelectsDefaultFromViewData()
  809. {
  810. // Arrange
  811. ViewDataDictionary<FooModel> vdd = new ViewDataDictionary<FooModel> { { "foo", "123456789" } };
  812. vdd.TemplateInfo.HtmlFieldPrefix = "MyPrefix";
  813. HtmlHelper<FooModel> helper = MvcHelper.GetHtmlHelper(vdd);
  814. // Act
  815. MvcHtmlString html = helper.DropDownListFor(m => m.foo, MultiSelectListTest.GetSampleIEnumerableObjects());
  816. // Assert
  817. Assert.Equal(
  818. @"<select id=""MyPrefix_foo"" name=""MyPrefix.foo""><option selected=""selected"" value=""123456789"">John</option>
  819. <option value=""987654321"">Jane</option>
  820. <option value=""111111111"">Joe</option>
  821. </select>",
  822. html.ToHtmlString());
  823. }
  824. // ListBox
  825. [Fact]
  826. public void ListBoxUsesExplicitValueIfNotProvidedInViewData()
  827. {
  828. // Arrange
  829. HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary());
  830. MultiSelectList selectList = new MultiSelectList(MultiSelectListTest.GetSampleAnonymousObjects(), "Letter", "FullWord", new[] { "A", "C" });
  831. // Act
  832. MvcHtmlString html = helper.ListBox("foo", selectList);
  833. // Assert
  834. Assert.Equal(
  835. @"<select id=""foo"" multiple=""multiple"" name=""foo""><option selected=""selected"" value=""A"">Alpha</option>
  836. <option value=""B"">Bravo</option>
  837. <option selected=""selected"" value=""C"">Charlie</option>
  838. </select>",
  839. html.ToHtmlString());
  840. }
  841. [Fact]
  842. public void ListBoxUsesExplicitValueIfNotProvidedInViewData_Unobtrusive()
  843. {
  844. // Arrange
  845. HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary());
  846. helper.ViewContext.ClientValidationEnabled = true;
  847. helper.ViewContext.UnobtrusiveJavaScriptEnabled = true;
  848. helper.ViewContext.FormContext = new FormContext();
  849. helper.ClientValidationRuleFactory = (name, metadata) => new[] { new ModelClientValidationRule { ValidationType = "type", ErrorMessage = "error" } };
  850. MultiSelectList selectList = new MultiSelectList(MultiSelectListTest.GetSampleAnonymousObjects(), "Letter", "FullWord", new[] { "A", "C" });
  851. // Act
  852. MvcHtmlString html = helper.ListBox("foo", selectList);
  853. // Assert
  854. Assert.Equal(
  855. @"<select data-val=""true"" data-val-type=""error"" id=""foo"" multiple=""multiple"" name=""foo""><option selected=""selected"" value=""A"">Alpha</option>
  856. <option value=""B"">Bravo</option>
  857. <option selected=""selected"" value=""C"">Charlie</option>
  858. </select>",
  859. html.ToHtmlString());
  860. }
  861. [Fact]
  862. public void ListBoxUsesViewDataDefaultValue()
  863. {
  864. // Arrange
  865. HtmlHelper helper = MvcHelper.GetHtmlHelper(_listBoxViewData);
  866. MultiSelectList selectList = new MultiSelectList(MultiSelectListTest.GetSampleStrings(), new[] { "Charlie" });
  867. // Act
  868. MvcHtmlString html = helper.ListBox("foo", selectList);
  869. // Assert
  870. Assert.Equal(
  871. @"<select id=""foo"" multiple=""multiple"" name=""foo""><option>Alpha</option>
  872. <option selected=""selected"">Bravo</option>
  873. <option>Charlie</option>
  874. </select>",
  875. html.ToHtmlString());
  876. }
  877. [Fact]
  878. public void ListBoxWithErrors()
  879. {
  880. // Arrange
  881. ViewDataDictionary viewData = GetViewDataWithErrors();
  882. HtmlHelper helper = MvcHelper.GetHtmlHelper(viewData);
  883. MultiSelectList list = new MultiSelectList(MultiSelectListTest.GetSampleStrings(), new[] { "Charlie" });
  884. // Act
  885. MvcHtmlString html = helper.ListBox("foo", list);
  886. // Assert
  887. Assert.Equal(
  888. @"<select class=""input-validation-error"" id=""foo"" multiple=""multiple"" name=""foo""><option>Alpha</option>
  889. <option selected=""selected"">Bravo</option>
  890. <option selected=""selected"">Charlie</option>
  891. </select>",
  892. html.ToHtmlString());
  893. }
  894. [Fact]
  895. public void ListBoxWithErrorsAndCustomClass()
  896. {
  897. // Arrange
  898. ViewDataDictionary viewData = GetViewDataWithErrors();
  899. HtmlHelper helper = MvcHelper.GetHtmlHelper(viewData);
  900. MultiSelectList selectList = new MultiSelectList(MultiSelectListTest.GetSampleStrings(), new[] { "Charlie" });
  901. // Act
  902. MvcHtmlString html = helper.ListBox("foo", selectList, new { @class = "foo-class" });
  903. // Assert
  904. Assert.Equal(
  905. @"<select class=""input-validation-error foo-class"" id=""foo"" multiple=""multiple"" name=""foo""><option>Alpha</option>
  906. <option selected=""selected"">Bravo</option>
  907. <option selected=""selected"">Charlie</option>
  908. </select>",
  909. html.ToHtmlString());
  910. }
  911. [Fact]
  912. public void ListBoxWithNameOnly()
  913. {
  914. // Arrange
  915. HtmlHelper helper = MvcHelper.GetHtmlHelper();
  916. helper.ViewData["foo"] = new MultiSelectList(MultiSelectListTest.GetSampleStrings(), new[] { "Charlie" });
  917. // Act
  918. MvcHtmlString html = helper.ListBox("foo");
  919. // Assert
  920. Assert.Equal(
  921. @"<select id=""foo"" multiple=""multiple"" name=""foo""><option>Alpha</option>
  922. <option>Bravo</option>
  923. <option selected=""selected"">Charlie</option>
  924. </select>",
  925. html.ToHtmlString());
  926. }
  927. [Fact]
  928. public void ListBoxWithAttributesDictionary()
  929. {
  930. // Arrange
  931. ViewDataDictionary viewData = new ViewDataDictionary();
  932. MultiSelectList selectList = new MultiSelectList(MultiSelectListTest.GetSampleStrings());
  933. //viewData["foo"] = selectList;
  934. HtmlHelper helper = MvcHelper.GetHtmlHelper(viewData);
  935. // Act
  936. MvcHtmlString html = helper.ListBox("foo", selectList, HtmlHelperTest.AttributesDictionary);
  937. // Assert
  938. Assert.Equal(
  939. @"<select baz=""BazValue"" id=""foo"" multiple=""multiple"" name=""foo""><option>Alpha</option>
  940. <option>Bravo</option>
  941. <option>Charlie</option>
  942. </select>",
  943. html.ToHtmlString());
  944. }
  945. [Fact]
  946. public void ListBoxWithAttributesDictionaryAndMultiSelectList()
  947. {
  948. // Arrange
  949. HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary());
  950. MultiSelectList selectList = new MultiSelectList(MultiSelectListTest.GetSampleStrings());
  951. // Act
  952. MvcHtmlString html = helper.ListBox("foo", selectList, HtmlHelperTest.AttributesDictionary);
  953. // Assert
  954. Assert.Equal(
  955. @"<select baz=""BazValue"" id=""foo"" multiple=""multiple"" name=""foo""><option>Alpha</option>
  956. <option>Bravo</option>
  957. <option>Charlie</option>
  958. </select>",
  959. html.ToHtmlString());
  960. }
  961. [Fact]
  962. public void ListBoxWithAttributesDictionaryOverridesName()
  963. {
  964. // DevDiv Bugs #217602:
  965. // SelectInternal() should override the user-provided 'name' attribute
  966. // Arrange
  967. ViewDataDictionary viewData = new ViewDataDictionary();
  968. MultiSelectList selectList = new MultiSelectList(MultiSelectListTest.GetSampleStrings());
  969. HtmlHelper helper = MvcHelper.GetHtmlHelper(viewData);
  970. // Act
  971. MvcHtmlString html = helper.ListBox("foo", selectList, new { myAttr = "myValue", name = "theName" });
  972. // Assert
  973. Assert.Equal(
  974. @"<select id=""foo"" multiple=""multiple"" myAttr=""myValue"" name=""foo""><option>Alpha</option>
  975. <option>Bravo</option>
  976. <option>Charlie</option>
  977. </select>",
  978. html.ToHtmlString());
  979. }
  980. [Fact]
  981. public void ListBoxWithEmptyNameThrows()
  982. {
  983. // Arrange
  984. HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary());
  985. // Act & Assert
  986. Assert.ThrowsArgumentNullOrEmpty(
  987. delegate { helper.ListBox(String.Empty, (MultiSelectList)null /* selectList */); },
  988. "name");
  989. }
  990. [Fact]
  991. public void ListBoxWithNullNameThrows()
  992. {
  993. // Arrange
  994. HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary());
  995. // Act & Assert
  996. Assert.ThrowsArgumentNullOrEmpty(
  997. delegate { helper.ListBox(null /* name */, (MultiSelectList)null /* selectList */); },
  998. "name");
  999. }
  1000. [Fact]
  1001. public void ListBoxWithNullSelectListUsesViewData()
  1002. {
  1003. // Arrange
  1004. HtmlHelper helper = MvcHelper.GetHtmlHelper();
  1005. helper.ViewData["foo"] = new MultiSelectList(MultiSelectListTest.GetSampleStrings(), new[] { "Charlie" });
  1006. // Act
  1007. MvcHtmlString html = helper.ListBox("foo", null);
  1008. // Assert
  1009. Assert.Equal(
  1010. @"<select id=""foo"" multiple=""multiple"" name=""foo""><option>Alpha</option>
  1011. <option>Bravo</option>
  1012. <option selected=""selected"">Charlie</option>
  1013. </select>",
  1014. html.ToHtmlString());
  1015. }
  1016. [Fact]
  1017. public void ListBoxWithObjectDictionary()
  1018. {
  1019. // Arrange
  1020. HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary());
  1021. MultiSelectList selectList = new MultiSelectList(MultiSelectListTest.GetSampleStrings());
  1022. // Act
  1023. MvcHtmlString html = helper.ListBox("foo", selectList, HtmlHelperTest.AttributesObjectDictionary);
  1024. // Assert
  1025. Assert.Equal(
  1026. @"<select baz=""BazObjValue"" id=""foo"" multiple=""multiple"" name=""foo""><option>Alpha</option>
  1027. <option>Bravo</option>
  1028. <option>Charlie</option>
  1029. </select>",
  1030. html.ToHtmlString());
  1031. }
  1032. [Fact]
  1033. public void ListBoxWithObjectDictionaryWithUnderscores()
  1034. {
  1035. // Arrange
  1036. HtmlHelper helper = MvcHelper.GetHtmlHelper(new ViewDataDictionary());
  1037. MultiSelectList selectList = new MultiSelectList(MultiSelectListTest.GetSampleStrings());
  1038. // Act
  1039. MvcHtmlString html = helper.ListBox("foo", selectList, HtmlHelperTest.AttributesObjectUnderscoresDictionary);
  1040. // Assert
  1041. Assert.Equal(
  1042. @"<select foo-baz=""BazObjValue"" id=""foo"" multiple=""multiple"" name=""foo""><option>Alpha</option>
  1043. <option>Bravo</option>
  1044. <option>Charlie</option>
  1045. </select>",
  1046. html.ToHtmlString());
  1047. }
  1048. [Fact]
  1049. public void ListBoxWithIEnumerableSelectListItem()
  1050. {
  1051. // Arrange
  1052. ViewDataDictionary vdd = new ViewDataDictionary { { "foo", MultiSelectListTest.GetSampleIEnumerableObjects() } };
  1053. HtmlHelper helper = MvcHelper.GetHtmlHelper(vdd);
  1054. // Act
  1055. MvcHtmlString html = helper.ListBox("foo");
  1056. // Assert
  1057. Assert.Equal(
  1058. @"<select id=""foo"" multiple=""multiple"" name=""foo""><option value=""123456789"">John</option>
  1059. <option value=""987654321"">Jane</option>
  1060. <option selected=""selected"" value=""111111111"">Joe</option>
  1061. </select>",
  1062. html.ToHtmlString());
  1063. }
  1064. [Fact]
  1065. public void ListBoxThrowsWhenExpressionDoesNotEvaluateToIEnumerable()
  1066. {
  1067. // Arrange
  1068. ViewDataDictionary vdd = new ViewDataDictionary { { "foo", 123456789 } };
  1069. HtmlHelper helper = MvcHelper.GetHtmlHelper(vdd);
  1070. // Act & Assert
  1071. Assert.Throws<InvalidOperationException>(
  1072. () => helper.ListBox("foo", MultiSelectListTest.GetSampleIEnumerableObjects()),
  1073. @"The parameter 'expression' must evaluate to an IEnumerable when multiple selection is allowed."
  1074. );
  1075. }
  1076. [Fact]
  1077. public void ListBoxThrowsWhenExpressionEvaluatesToString()
  1078. {
  1079. // Arrange
  1080. ViewDataDictionary vdd = new ViewDataDictionary { { "foo", "123456789" } };
  1081. HtmlHelper helper = MvcHelper.GetHtmlHelper(vdd);
  1082. // Act & Assert
  1083. Assert.Throws<InvalidOperationException>(
  1084. () => helper.ListBox("foo", MultiSelectListTest.GetSampleIEnumerableObjects()),
  1085. @"The parameter 'expression' must evaluate to an IEnumerable when multiple selection is allowed."
  1086. );
  1087. }
  1088. [Fact]
  1089. public void ListBoxWithListOfSelectListItemSelectsDefaultFromViewData()
  1090. {
  1091. // Arrange
  1092. ViewDataDictionary vdd = new ViewDataDictionary { { "foo", new string[] { "123456789", "111111111" } } };
  1093. HtmlHelper helper = MvcHelper.GetHtmlHelper(vdd);
  1094. // Act
  1095. MvcHtmlString html = helper.ListBox("foo", MultiSelectListTest.GetSampleListObjects());
  1096. // Assert
  1097. Assert.Equal(