尝试通过视图模型填充选择列表。 查看型号:
public class CountryCity
{
public IEnumerable<SelectListItem> Countries { get; set; }
public SelectList Cities { get; set; }
}
控制器:
public class HomeController : Controller
{
private CountriesContext context = new CountriesContext();
public ActionResult Index()
{
CountryCity cc = new CountryCity();
var countries = from c in context.Countries
orderby c.Name
select c;
cc.Countries = new SelectList(countries,"CountryID", "Name");
return View(cc);
}
}
查看:
@using CascadingDropdowns.Models.ViewModels
@model CountryCity
<h2>Index</h2>
<div>
@using (Html.BeginForm())
{
@Html.DropDownList("CountryID", Model.Countries, "Please select country");
@Html.DropDownList("City", new SelectList(string.Empty, "City", "<city>"), "Please select a city", new { @disabled = "disabled" }));
}
</div>
这会产生错误:
C:\Users\Jakov\AppData\Local\Temp\Temporary ASP.NET Files\root\aad61407\c9799020\App_Web_index.cshtml.a8d08dba.lv9oabqd.0.cs(97,58): error CS1513: } expected
一切看起来都很好。
答案 0 :(得分:4)
您需要删除以下行中的一个右括号:
@Html.DropDownList("City", new SelectList(string.Empty, "City", "<city>"), "Please select a city", new { @disabled = "disabled" }));
应该是
@Html.DropDownList("City", new SelectList(string.Empty, "City", "<city>"), "Please select a city", new { @disabled = "disabled" });