... so is there a way to pass SelectListItem values ββas long as strings?
No. And it makes no sense to do this, since when rendering it is just HTML, which has no concept of long .
If we have an action
public ActionResult Test() { var dictionary = new Dictionary<int, string> { { 1, "One" }, { 2, "Two" }, { 3, "Three" } }; ViewBag.SelectList = new SelectList(dictionary, "Key", "Value"); return this.View(); }
and the following view of "Test.cshtml":
@using (Html.BeginForm()) { @Html.DropDownList("id", ((SelectList)ViewBag.SelectList), "All") <input type="submit" value="Go" /> }
HTML generated
<form action="/home/test" method="post"> <select id="id" name="id"> <option value="">All</option> <option value="1">One</option> <option value="2">Two</option> <option value="3">Three</option> </select> <input type="submit" value="Go"> </form>
and when we send this action, the text of your number is effectively parsed back to the desired type using model binding
[HttpPost] public ActionResult Test(int? id) { var selectedValue = id.HasValue ? id.ToString() : "All"; return Content(String.Format("You selected '{0}'", selectedValue)); }
And it works as you would expect.
dav_i source share