Hi, that's why I'm pretty new to MVC3 and Razor, and I have been trying to trick him in the last few days. My project architect was tasked with creating a helper method that sorts a drop-down list in an MVC view. I have a view that retrieves various data from the controller, and I am returning some values that I want to display in the drop down list. I was told not to sort it in the controller, and also to pass the field that we want to sort into a helper method. I could do this as shown below, but the architect wants to keep the view without clear code:
@Html.DropDownListFor(model => model.StudyName, new SelectList(ViewBag.StudyTypes, "Value", "Text").OrderBy(l => l.Text))
So, I created a code sample and some extension methods to try to make it work. My idea is to replicate an existing Html.DropDownList method and allow the transfer of "object htmlAttributes" so that I can set the style as part of the method call.
Here is my code so far. I am returning data for a dropdown in the ViewBag.StudyTypes in the Edit Controller method:
public ActionResult Edit(int id)
{
IEnumerable<SelectListItem> mySelectList = new List<SelectListItem>();
IList<SelectListItem> myList = new List<SelectListItem>();
for (int i = 0; i < 5; i++)
{
myList.Add(new SelectListItem()
{ Value = i.ToString(), Text = "My Item " + i.ToString(), Selected = i == 2 }
);
}
mySelectList = myList;
ViewBag.StudyTypes = mySelectList;
StudyDefinition studydefinition = db.StudyDefinitions.Find(id);
return View(studydefinition);
}
Here is my view code:
@model MyStudyWeb.Models.StudyDefinition
@using MyStudyWeb.Helpers
@{
ViewBag.Mode = "Edit";
}
<div>
@Html.DropDownListSorted(new SelectList(ViewBag.StudyTypes, "Value", "Text"))<br />
@Html.DropDownListSorted("MyList", new SelectList(ViewBag.StudyTypes, "Value", "Text"))<br />
</div>
Finally, below are the extension methods I'm trying to get. The first extension method does nothing, I just get empty space at this point in the view. The second method seems to work, but it is ugly. For the third method, I don’t know how to specify the "order by" parameter, since OrderBy in IEnumerable expects a Linq expression.
namespace StudyDefinition.Helpers
{
public static class HtmlHelperExtensions
{
public static SelectList DropDownListSorted(this HtmlHelper helper, IEnumerable<SelectListItem> selectList)
{
var x = new SelectList(selectList.ToList()).OrderBy(l => l.Text);
return x as SelectList;
}
public static IHtmlString DropDownListSorted(this HtmlHelper helper, string name, SelectList selectList)
{
StringBuilder output = new StringBuilder();
(selectList).OrderBy(l => l.Text);
output.Append("<select id=" + name + " name=" + name + ">");
foreach (var item in selectList)
{
output.Append("<option value=" + item.Value.ToString() + ">" + item.Text + "</option>");
}
output.Append("</select>");
return MvcHtmlString.Create(output.ToString());
}
public static IHtmlString DropDownListSorted(this HtmlHelper helper, string name, SelectList selectList, string orderBy)
{
StringBuilder output = new StringBuilder();
(selectList).OrderBy(l => l.Text);
output.Append("<select id=" + name + " name=" + name + ">");
foreach (var item in selectList)
{
output.Append("<option value=" + item.Value.ToString() + ">" + item.Text + "</option>");
}
output.Append("</select>");
return MvcHtmlString.Create(output.ToString());
}
}
}
, , , , , , .
- - , :)