我是 MVC 的新手,但一直在稳步取得进展,但是我最近遇到了一个问题,尽管阅读了许多关于类似主题的类似帖子,但我似乎无法克服。
我有一个模型如下(为简洁起见进行了简化)
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual Address Address { get; set; }
我有一个强类型 View 如下:
@model JFS.Data.Model.Supplier
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Supplier Address</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Address.AddressLine1)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Address.AddressLine1)
@Html.ValidationMessageFor(model => model.Address.AddressLine1)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Address.Country)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Address.Country)
@Html.ValidationMessageFor(model => model.Address.Country)
</div>
</fieldset>
我有一个共享的 Country 字段的 EditorTemplate,如下所示:
@model JFS.Data.Model.Address
@using System.Globalization
@Html.DropDownListFor(o => o.Country, GetCountries(Model), "Please select")
@functions
{
private static IEnumerable<SelectListItem> GetCountries(object country)
{
var regions = CultureInfo.GetCultures(CultureTypes.SpecificCultures)
.Select(cultureInfo => new RegionInfo(cultureInfo.LCID))
.OrderBy(r => r.EnglishName)
.Distinct()
.ToList();
return new SelectList(regions, "TwoLetterISORegionName", "EnglishName", country);
}
}
我理解这个问题,但不确定如何最好地克服它,任何建议将不胜感激。
请您参考如下方法:
虽然@ProNotion 已经回答了您的特定情况,但我之前在将 null 传递到 View 或模板时遇到过这个问题。它可能会帮助其他人走这条路。
