我想在我的視圖中填充dropdownlistfor,我不知道它正確的lambda表達式的工作。請參見下面的代碼:MVC 4 - 與型號列表填充下拉列表
@model Website.Models.modelTeamSelect
<h2>MatchSelect.cshtml</h2>
@using (Ajax.BeginForm("_PartialTeams",
new
{
model = this.Model
},
new AjaxOptions
{
HttpMethod = "POST",
UpdateTargetId = "divMatchScheduler",
InsertionMode = InsertionMode.Replace
}))
{
<div id="divMatchScheduler">
@Html.LabelFor(m => m.modelMatch.HomeTeam)
@Html.DropDownListFor(m => m.modelMatch.HomeTeam, new SelectList
{
Items = Model.teams.Select(t => t.TeamName)
})
@Html.LabelFor(m => m.modelMatch.AwayTeam)
@Html.LabelFor(m => m.modelMatch.MatchDate)
@Html.TextBoxFor(m => m.modelMatch.MatchDate)
</div>
<input type="submit" value="Add Match" name="btnSubmit" />
}
這個語法錯誤
@Html.DropDownListFor(m => m.modelMatch.HomeTeam, new SelectList
{
Items = Model.teams.Select(t => t.TeamName)
})
我的模型正在使用的視圖和收集它包含
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Website.Models
{
public class modelMatch
{
public Guid MatchID { get; set; }
public Guid AwayTeamID { get; set; }
public string AwayTeam { get; set; }
public Guid HomeTeamID { get; set; }
public string HomeTeam { get; set; }
public DateTime MatchDate { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Website.Models
{
public class modelTeamSelect
{
public modelTeamSelect()
{
teams = new List<modelTeam>();
team = new modelTeam();
modelMatch = new Models.modelMatch();
modelMatches = new List<modelMatch>();
}
public List<modelTeam> teams { get; set; }
public modelTeam team { get; set; }
public modelMatch modelMatch { get; set; }
public List<modelMatch> modelMatches { get; set; }
public string message { get; set; }
}
}
摘要
如何使用我的模型中的列表集合來填充dropdownlist?
謝謝。這是我想要的。 –