0
私は非常にasp.netで新しいです。今日私はドロップダウンリストをソートしたい。 これは編集の私のコントローラである:how sort DropDownListの項目アルファベット順asp.net
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
User user = db.Users.Find(id);
if (user == null)
{
return 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);
return View(user);
}
そして、これはどのように私はアルファベット順に私の@ Html.DropDownListをソートすることができ、私の編集ビュー
@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>
のですか?私は私のコメントで述べたよう
を 'ViewBag.DefaultServiceId =新しいSelectListの(db.Services.OrderBy(S => s.Name )、 "Id"、 "Name"、user.DefaultServiceId); ' –
@QuentinRoger - ありがとうございました! – BinaryTie