DropdownList ASP.net MVC5 的问题

Problems with DropdownList ASP.net MVC5

关于下拉列表的这个基本问题,我遇到了很多问题我做了很多 Whosebug 中描述的方法,但都没有用我将这样总结代码。我只想简单地创建一个保存到数据库的下拉列表。

这是我做的 默认型号

public class DefaultModel
  {
    public int Id {get; set;}
    //other things
    public CountryList SelectedCountry {get; set;}
  }
  public DefaultDbContext : DbContext
   {
      public DbSet<DefaultModel> DefaultModels {get; set;}
      public DbSet<CountrtyList> CountryLists {get; set;}
   }

国家列表型号

public class CountryList
  {
    public byte Id {get; set;}
    public string name {get; set;} //country name
    public DefaultModel DefaultModel {get; set;}
  }

ViewModel

 public class CountryListViewModel
 {
   public byte Id {get; set;}
   public List<CountryList> CountryList {get; set;}

默认控制器 不是家庭控制器

 private DefaultDbContext = db;
 public HomeController
 {
   db = new DefaultDbContext;
 }

 public ActionResult Index()
  {
        var countryLists= _context.CountryLists.ToList();
        var viewModel = new CountryListViewModel
        {
            Default = new DefaultModel(),
            CountryLists = countrylists
        };

        return View(viewModel);
  }

查看

 @model Default.ViewModels.CountryListViewModel
 <div class="form-group">
 @Html.LabelFor(model => 
  model.TransactionModel.SelectedCountry.CountryLists
   , new { @class = "control-label col-md-2" })
    <div class="col-md-10">
   @Html.DropDownListFor(
   model =>    model.TransactionModel.SelectedCountry.CountryLists
   , new     SelectList(Model.CountryList, "Id, Name"), "Select Country")
    </div>
</div>
 <div class="form-group">
    <div class="col-md-offset-2 col-md-10">
        <input type="submit" value="Create" class="btn btn-default" />
    </div>
</div>

迁移 添加国家/地区

 Sql("INSERT INTO COUNTRYLISTS (Id, Name) VALUES(1, UK)");

错误

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

我只是想学习如何从头开始在 MVC5 中创建下拉列表,我浪费了很多时间并且多次尝试都失败了。我看到使用根本不起作用的方法的教程我不知道哪里出了问题或我应该做什么。

模态

namespace WebApp.Models
{
    public class CountryList
    {
        public byte Id { get; set; }
        public string name { get; set; } //country name
        //public DefaultModel DefaultModel { get; set; }
    }
    public class CountryListViewModel
    {
        public string CountryLists { get; set; }
        public List<CountryList> CountryList { get; set; }
    }
}

控制器

`

        List<CountryList> countrylists = new List<CountryList>();

        //I am using static list items. Just change this to get data from entity framework;

        countrylists.Add(new CountryList  //pass one default list Item here so that your list is never null.
        {
            Id = 0,
            name = "Select Country"
        });

        countrylists.Add(new CountryList
        {
            Id = 1,
            name = "UK"
        });
        var viewModel = new CountryListViewModel
        {
            //Default = new DefaultModel(),
            CountryList=countrylists,
            CountryLists="Country List"
        };

        return View(viewModel);`

查看

<div class="content">
    @Html.LabelFor(model => model.CountryList, new { @class = "control-label col-md-2" })

    <div class="col-md-10">

        @Html.DropDownListFor(model => model.CountryList, new SelectList(Model.CountryList, "id", "name"))

    </div>

    <br />
    <br />
    <br />
    <a href="http://helpmepal.org">Help Me Pal</a>
</div>