如何连接DropDownListFor()的FirstName和LastName,我正在尝试的不是我的代码。
查看模型
public class ViewModelUser
{
public SelectList User { get; set; }
[Required]
public string SelectedUID { get; set; }
}
控制器
public ActionResult GiveReview()
{
ViewModelUser viewModelUser = new ViewModelUser();
viewModelUser.User = new SelectList(dbContext.Users.Where(u => u.Deleted != true).Select(us => new { us.FirstName, us.LastName, us.UID }), "UID", "FirstName" + "LastName");
return View(viewModelUser );
}
查看
@model WebApplication22.ViewModels.ViewModelUser
@using (Html.BeginForm("GiveReview", "ProvideReview", FormMethod.Post, new { Id = "ratingsForm" }))
{
@Html.AntiForgeryToken()
<div class="form-group">
<label class="profile-name">Select User</label>
@Html.DropDownListFor(m => m.SelectedUID, (IEnumerable<SelectListItem>)Model.User, "-Select User-", new { @class = "form-control" })
</div>
<button type="submit" class="btn btn btn-primary">
Submit
</button>
}
任何帮助将不胜感激。
答案 0 :(得分:2)
您需要连接Select
子句
viewModelUser.User = new SelectList(dbContext.Users
.Where(u => u.Deleted != true).ToList()
.Select(u => new { FullName = String.Format("{0} {1}", u.FirstName, u.LastName), UID = u.UID }),
"UID", "FullName");