asp.net mvc将选定的dropdownlist值绑定到模型

时间:2016-11-04 00:26:49

标签: c# asp.net asp.net-mvc

尝试将所选值从下拉列表绑定到模型时,我收到以下错误:

An exception of type 'System.ArgumentNullException' occurred in System.Web.Mvc.dll but was not handled in user code. Additional information: Value cannot be null.

这是我的代码:

模型:

public class Countries
{
    public string CountryCode { get; set; }
    public string Country { get; set; }
}

public class ViewModel
{
    public List<Countries> CountryDDL { get; set; }
    public string Country { get; set; }
}

CONTROLLER

public ActionResult Register()
{
    var model = new ViewModel();
    model.CountryDDL = new List<Countries>(_uiRepository.GetAllCountries());

    return View(model);
}

查看

@Html.LabelFor(m => m.Country)
@Html.DropDownListFor(m => m.Country, new SelectList(Model.CountryDDL, "CountryCode", "Country"), "--Select a Country--", new { @class = "form-control" })

0 个答案:

没有答案