2011-03-12 3 views
0

저는 파트너라는 클래스가 있는데, 뷰에서 모든 정보를 제출해야합니다.MVC3 (asp.net, C#)의 드롭 다운 목록에서 선택한 결과를 제출하십시오.

드롭 다운 목록은 데이터 집합으로 채워 져야하며 제출시 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); 
} 
나는 아래로 내 드롭을 만드는 방법에 대해 갈 것이라고 어떻게

보기, 내보기 모델은 파트너 클래스에서 bassed?

답변

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") 
) 
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; 
     } 
관련 문제