如何使用MVC4将硬编码的下拉列表值绑定到数据库中?

时间:2014-12-19 07:48:32

标签: asp.net-mvc-4

我创建了下拉列表,其中填充了国家名称和国家/地区名称的值是硬编码的。现在我想要选择的国家名称必须存储在数据库中。有人可以帮我吗?我尝试了Get方法中的代码,

SelectListItem item;
List<SelectListItem> CountryList = new List<SelectListItem>();
item = new SelectListItem();
item.Text = "India";
item.Value = "1";
myList.Add(item);
item = new SelectListItem();
item.Text = "Italy";
item.Value = "0";
CountryList.Add(item);
ViewBag.country = CountryList;
return View();

型号:

public class Createcountry
{
[Table("mytable", Schema = "public")]
    public class Create
    {


            [Key]
            [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
            public int regionid { get; set; }
            public string countryname { get; set; }
}
}

查看:

@Html.DropDownListFor(m => m.countryname, (IEnumerable<SelectListItem>)ViewBag.country, "select country");

0 个答案:

没有答案