0
ビューモデルによって設定されたドロップダウンリストを作成しようとしていますが、HTTPPOSTで、選択した値を取得しようとしましたが、どんな助けもありがとう。View Model DropDownListForでバインドされていません
ビューモデル
public class TransferViewModel
{
public IEnumerable<User> Users { get; set; }
public string SortOrder { get; set; }
public SelectList Departments { get; set; }
public SelectList Sites { get; set; }
public SelectListItem SelectedDepartment { get; set; }
public SelectListItem SelectedSite { get; set; }
public User SelectedUser { get; set; }
}
コントローラ
public ActionResult Sort(TransferViewModel model)
{
var users = from s in _db.Users select s;
var departments = new SelectList(_db.Departments);
var sites = new SelectList(_db.Sites);
model.Departments = departments;
model.Sites = sites;
if (model.SelectedDepartment != null)
{
users = users.Where(s => s.Department.ID == Convert.ToInt32(model.SelectedDepartment));
}
else if (model.SelectedSite != null)
{
users = users.Where(s => s.Site.ID == Convert.ToInt32(model.SelectedSite));
}
switch (model.SortOrder)
{
case "name_desc":
users = users.OrderByDescending(s => s.LName);
break;
case "dept":
users = users.OrderBy(s => s.Department.Name);
break;
case "dept_desc":
users = users.OrderByDescending(s => s.Department.Name);
break;
case "site":
users = users.OrderBy(s => s.Site.Name);
break;
case "site_desc":
users = users.OrderByDescending(s => s.Site.Name);
break;
default:
users = users.OrderBy(s => s.LName);
break;
}
model.Users = users;
model.Departments = new SelectList(_db.Departments, "ID", "Name", model.SelectedDepartment);
model.Sites = new SelectList(_db.Sites,"ID","Name", model.SelectedSite);
return View(model);
}
ビュー
@using (Html.BeginForm("Sort", "Transfer", FormMethod.Post))
{
<p>
Display by @Html.DropDownListFor(model => model.SelectedDepartment, Model.Departments, htmlAttributes: new { @class = "form-control"})
Department
</p>
<p>
or by Location: @Html.DropDownListFor(model => model.SelectedSite, Model.Sites, htmlAttributes: new { @class = "form-control"})
</p>
<p>
<input type="submit" value="Search"/>
</p>
}