使用带有List <selectlistitem>

时间:2017-11-17 12:03:13

标签: asp.net-mvc

我是MVC的新手,想使用Model在DropDownList中绑定数据。但我不确定如何进入。 以下是我的代码

- 模型

public string Name { get; set; }
    public string Qualification { get; set; }
    public string CountryID { get; set; }
    public string StateID { get; set; }
    public string CityID { get; set; }
    public List<SelectList> CountryList { get; set; }
    //public List<SelectListItem> Country { get; set; }
    public List<SelectList> StateList { get; set; }
    public List<SelectList> CityList { get; set; }

- 查看

<tr>
        <td>@Html.LabelFor(s => s.Name)</td>
        <td>@Html.TextBoxFor(s => s.Name, new { @class = "form-control" })</td>
    </tr>
    <tr>
        <td>@Html.LabelFor(s => s.Qualification)</td>
        <td>@Html.TextBoxFor(s => s.Qualification, new { @class = "form-control" })</td>
    </tr>
    <tr>
        <td>@Html.LabelFor(s => s.Country)</td>
        <td>@Html.DropDownList("Country", new SelectList(Model.CountryList), "Select Country", new { @Class = "form-control" })</td>
    </tr>

- 控制器

 public ActionResult DisplayView(EmployeeDetails.Models.DisplayModel dm)
    {
        EmployeeDetails.Models.DisplayModel obj = new EmployeeDetails.Models.DisplayModel();
        List<SelectListItem> CountryList = obj.CountryDetails(dm);
        //var CountryList = obj.CountryDetails(dm);

        obj.CountryList = new SelectList(CountryList, "Value", "Text");

        return View();
    }

在模型中,我有一个方法(CountryDetails)将COuntrylist作为List从数据库返回。

我在下面的行中收到错误,无法为模型中的列表指定值。

 obj.CountryList = new SelectList(CountryList, "Value", "Text");

请帮我在Dropdown中正确显示值。

1 个答案:

答案 0 :(得分:0)

我刚刚复制了您的问题,并进行了一些更改以使其运行。

在模型i中,我已从web-ext run --pref='datareporting.policy.firstRunURL=' 更改为list<SelectList> 之后我在控制器中有PopulateCountry下拉列表硬编码数据,您可以通过将其替换为数据库来更改它。

在使用jquery ajax填充下拉列表后,我已经填充了州和城市下拉列表。

  

模型

List<SelectListItem>
  

控制器代码

public class DisplayModel
{
    [Display(Name = "Choose Country")]
    public string Country { get; set; }
    public string Name { get; set; }
    public string Qualification { get; set; }
    public string CountryID { get; set; }
    public string StateID { get; set; }
    public string CityID { get; set; }
    public List<SelectListItem> CountryList { get; set; }
    public List<SelectListItem> StateList { get; set; }
    public List<SelectListItem> CityList { get; set; }
}
  

查看

 public class DemoController : Controller
{
    // GET: Demo
    public ActionResult Index()
    {
        DisplayModel obj = new DisplayModel();
        obj.CountryList = PopulateCountry();
        return View(obj);
    }

    private static List<SelectListItem> PopulateCountry()
    {
        List<SelectListItem> li = new List<SelectListItem>();
        li.Add(new SelectListItem { Text = "Select", Value = "0" });
        li.Add(new SelectListItem { Text = "India", Value = "1" });
        li.Add(new SelectListItem { Text = "Srilanka", Value = "2" });
        return li;
    }

    public JsonResult GetStates(string id)
    {
        List<SelectListItem> states = new List<SelectListItem>();
        switch (id)
        {
            case "1":
                states.Add(new SelectListItem { Text = "Select", Value = "0" });
                states.Add(new SelectListItem { Text = "ANDAMAN & NIKOBAR ISLANDS", Value = "1" });
                states.Add(new SelectListItem { Text = "MAHARASHTRA", Value = "2" });
                break;
        }
        return Json(new SelectList(states, "Value", "Text"));
    }

    public JsonResult GetCity(string id)
    {
        List<SelectListItem> City = new List<SelectListItem>();
        switch (id)
        {
            case "2":
                City.Add(new SelectListItem { Text = "Select", Value = "0" });
                City.Add(new SelectListItem { Text = "MUMBAI", Value = "1" });
                City.Add(new SelectListItem { Text = "PUNE", Value = "2" });
                City.Add(new SelectListItem { Text = "KOLHAPUR", Value = "3" });
                City.Add(new SelectListItem { Text = "RATNAGIRI", Value = "4" });
                City.Add(new SelectListItem { Text = "NAGPUR", Value = "5" });
                City.Add(new SelectListItem { Text = "JALGAON", Value = "6" });
                break;
        }
        return Json(new SelectList(City, "Value", "Text"));
    }
}

@model WebApplication1.Models.DisplayModel
@{
    ViewBag.Title = "Index";
}
<table>
    <tr>
        <td>@Html.LabelFor(s => s.Country)</td>
        <td>@Html.DropDownList("Country", Model.CountryList, "Select Country", new { @Class = "form-control" })</td>
    </tr>

    <tr>
        <td>State</td>
        <td>@Html.DropDownList("State", new SelectList(string.Empty, "Value", "Text"), "Please select a State", new { style = "width:250px", @class = "form-control" }) </td>
    </tr>

    <tr>
        <td>City</td>
        <td>@Html.DropDownList("city", new SelectList(string.Empty, "Value", "Text"), "Please select a city", new { style = "width:250px", @class = "form-control" })   </td>
    </tr>
</table>