在MVC3(asp.net,c#)中下拉列表并提交所选结果

时间:2011-03-12 16:54:39

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

我有一个名为Partner的课程,我需要提交View中的所有信息。

下拉列表需要由数据集填充,在提交时我想用选定的下拉项填充SelectedHeardAboutText。

这是我的班级:

public class Partner
{
        public string Name { get; set; }
        public string SelectedHeardAboutText { get; set; }
        public IEnumerable<SelectListItem> HowDidYouHear { get; set; }
}

这是我的PartnerController:

公共ActionResult合作伙伴()     {

    var hear = db.HowDidYouHears.ToList();
    var partner = new Partner();
    ViewBag.Hear = hear;

    return View(partner);
} 

我如何在视图上创建下拉列表,我的视图模型基于Partner类?

2 个答案:

答案 0 :(得分:1)

@Html.DropDownList(
    "SelectedHeardAboutText", 
    new SelectList((IEnumerable<Hear>)ViewBag.Hear, "Value", "Text")
)

显然这很难看,我不推荐它。甚至不知道我为什么发布它。可能会说永远不应该使用它。

这是正确的方法(通过使用强类型视图):

public ActionResult Partner() 
{
    var partner = new Partner
    {
        HowDidYouHear = db.HowDidYouHears.Select(x => new SelectListItem
        {
            Value = x.Id, // this will be used as value
            Text = x.SomeTextProperty // this will be used as text
        })
    };
    return View(partner);
} 

并在您看来:

@Html.DropDownListFor(
    x => x.SelectedHeardAboutText, 
    new SelectList(Model.Hear, "Value", "Text")
)

答案 1 :(得分:0)

<http://dotnetpools.com/Article/ArticleDetiail/?articleId=48&title=Binding%20Dropdownlist%20In%20MVC3%20Using%20C#t;>

## razor ##

    @Html.DropDownList("Country", new SelectList(Model.CountryList, "Value", "Text", Model.CountryList.SelectedValue), new { @id = "ddlist", @data_role = "none", style = "color: #fff; background-color: #fff; font-family: Arial, Helvetica, sans-serif; font-size: 10px; color: #333333; margin-left:3px; width:100%; height:20px;" })


## model ##

     public class somename
      {
       public SelectList CountryList { get; set; }
      }
       public class Country
        {
            public string ID { get; set; }
            public string Name { get; set; }
        }

## Controller ##

       public ActionResult index()
        {
          List<Country> objcountry = new List<Country>();
          objcountry = GetCountryList();
          SelectList objlistofcountrytobind = new SelectList(objcountry, "ID", "Name", 0);
          model.CountryList = objlistofcountrytobind;       
          return View(model);
        }


      [HttpPost]
      public ActionResult Analyze(AnalyzeModels model)
       {
          List<Country> objcountry = new List<Country>();
          objcountry = GetCountryList();
          SelectList objlistofcountrytobind = new SelectList(objcountry, "ID", "Name", 0);
          model.CountryList = objlistofcountrytobind;
          return View(model);
       }
              **************************************************
## function for GetCountryList##
        public List<Country> GetCountryList()
        {
            DataTable reportDetailsTable = objCommon.GetDetails("tablename");

            List<Country> objcountrty = new List<Country>();
            int s = reportDetailsTable.Rows.Count;
            for (int i = 0; i < s; i++)
            {
                string d1 = reportDetailsTable.Rows[i][1].ToString();
                string d2 = reportDetailsTable.Rows[i][10].ToString();
                objcountrty.Add(new Country { ID = d1, LName = d2 });
            }
            return objcountrty;
        }