I am quite new In asp.net. Today I want to sort my drop down list. This is my controller of editing:
public ActionResult Edit(int? id)
{
if (id == null)
{
retu new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
User user = db.Users.Find(id);
if (user == null)
{
retu HttpNotFound();
}
ViewBag.DefaultCustomerProjectId = new SelectList(db.CustomerProjects, "Id", "Name", user.DefaultCustomerProjectId);
ViewBag.DefaultServiceId = new SelectList(db.Services, "Id", "Name", user.DefaultServiceId);
ViewBag.CustomerId = new SelectList(db.Customers, "Id", "Name", user.CustomerId);
retu View(user);
}
And this is my edit view
@model TimeReportingLib.User
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>User</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.Id)
<div class="form-group">
@Html.LabelFor(model => model.DefaultServiceId, "DefaultServiceId", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("DefaultServiceId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.DefaultServiceId, "", new { @class = "text-danger" })
</div>
</div>
How can I sort my @Html.DropDownList alphabetically?
