无法从模型中填充选择列表

时间:2016-04-22 23:35:12

标签: asp.net asp.net-mvc asp.net-mvc-5

细节:

  • ASP.NET 4.5.2
  • MVC 5
  • 没有数据库,没有登录,只是普通的前端网络广告牌。
  • 不需要100%准确的国家列表,只要加拿大和美国这样的地方不缺,就足够了。

我似乎想把自己绑在这里打结,试图让一个国家列表显示在一个下拉选择列表中。

我的模特:

[DisplayName("Country")]
public string Country { get; set; }
private IEnumerable<SelectListItem> _CountryList;
public IEnumerable<SelectListItem> CountryList {
  get {
    List<string> CultureList = new List<string>();
    CultureInfo[] getCultureInfo = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
    foreach(CultureInfo getCulture in getCultureInfo) {
      RegionInfo GetRegionInfo = new RegionInfo(getCulture.LCID);
      if(!CultureList.Contains(GetRegionInfo.EnglishName)) {
        CultureList.Add(GetRegionInfo.EnglishName);
      }
    }
    return CultureList.OrderBy(x => x.ToString()).Select(x => new SelectListItem { Value = x.ToString(), Text = x.ToString() }).ToList();
  }
  set { _CountryList = value; }
}

我的观点:

@Html.DropDownListFor(model => model.Country, new SelectList(Model.CountryList, "Value", "Text"), " « ‹ Choose your country › » ", htmlAttributes: new { @class = "form-control" })

返回的错误消息是令人愤怒的,

System.NullReferenceException: Object reference not set to an instance of an object.

这是代码,“你好了,祝你好运,因为我不是在告诉它。”

我已经尝试用手工制作的选择列表手动填充模型,为零效果。我在哪里错了?

编辑1:

我现在试过了,

RegionInfo country = new RegionInfo(new CultureInfo("en-US", false).LCID);
List<SelectListItem> countryNames = new List<SelectListItem>();

//To get the Country Names from the CultureInfo installed in windows
foreach(CultureInfo cul in CultureInfo.GetCultures(CultureTypes.SpecificCultures)) {
  country = new RegionInfo(new CultureInfo(cul.Name, false).LCID);
  countryNames.Add(new SelectListItem() { Text = country.DisplayName, Value = country.DisplayName });
}

//Assigning all Country names to IEnumerable
IEnumerable<SelectListItem> nameAdded = countryNames.GroupBy(x => x.Text).Select(x => x.FirstOrDefault()).ToList().OrderBy(x => x.Text);
return nameAdded;

没有成功。同样的结果 - “你的傻瓜”。

编辑2:

型号:

[DisplayName("Country:")]
public string Country { get; set; }
public IEnumerable<SelectListItem> CountryList { get; set; }

查看:

@Html.DropDownListFor(model => model.Country, Model.CountryList, " « ‹ Choose your country › » ", htmlAttributes: new { @class = "form-control" })

控制器:

namespace Project.Controllers {
  public class HomeController : Controller {
    public ActionResult Index() {
      return View();
    }

    public ActionResult Demo() {
      var model = new DemoViewModel() {
        model.CountryList = CountryList.getCountries()
      };
      return View(model);
    }
  }
//dummy content, just to try to "populate" ****ANYTHING****
  public class CountryList {
    public static IEnumerable<SelectListItem> getCountries() {
      IList<SelectListItem> items = new List<SelectListItem> {
      new SelectListItem{Text = "United States", Value = "B"},
      new SelectListItem{Text = "Canada", Value = "B"},
      new SelectListItem{Text = "United Kingdom", Value = "B"},
      new SelectListItem{Text = "Texas", Value = "B"},
      new SelectListItem{Text = "Washington", Value = "B"}
      };
      return items;
    }
  }
}

您会看到我尝试将CountryList.getCountries()附加到model.CountryList

的位置
Cannot initialize 'DemoViewModel' with a collection initializer because it does not implement 'System.Collections.IEnumerable'

撕掉头发

1 个答案:

答案 0 :(得分:3)

假设CountryList.getCountries()返回SelectListItem

的集合

要么

var model = new DemoViewModel(); 
model.CountryList = CountryList.getCountries();
return View(model);

在这里,您要创建DemoviewModel类的对象并明确设置CountryList属性。

或者

var model = new DemoViewModel {
                                   CountryList = CountryList.getCountries()
                              };
 return View(model);

这称为对象初始化。在这里,您将创建一个对象并初始化CountryList属性值以及该值。

假设您的DemoViewModel类有另一个属性来保存所选项目。

public class DemoViewModel
{
  public List<SelectListItem> CountryList { set;get;}
  public string Country { set;get;}
}

现在在您的视图中强烈键入此视图模型,您可以使用DropDownListFor帮助方法。

@model DemoViewModel
@using(Html.BeginForm())
{
  @Html.DropDownListFor(s=>s.Country,Model.CountryList)
  <input type="submit" />
}
相关问题