C#/asp.net/mvc의 초보자로서 View의 Html.DropDownList에서 개체를 가져 오는 방법을 찾을 수 없습니다.리스트에서 객체를 얻는 방법 DropDownlistFor에 <typeObject>?
public ActionResult Index() {
MyViewModel mvm=new MyViewModel();
mvm.ListOfSomething=(my code to populate the List);
return View(vm);
}
보기는 여기에 있습니다 : 내가 컨트롤러 내 목록 <>를 채울
public class Something {
public int Id {get;set;}
public string Name {get;set;}
public int AnotherProperty {get;set;}
}
:
public class MyViewModel {
public List<Something> ListOfSomething {get;set;} // A list of Somethings
public Something TheSomethingToGet {get;set;} // The object I want to get
}
은 "뭔가"개체가, 예를 들면 : 여기 내 뷰 모델입니다 :
@model Project.ViewModels.MyViewModel
@Html.LabelFor(m => m.ListOfSomething)
@Html.DropDownListFor(m => m.ListOfSomething, new SelectList(Model.ListOfSomething, "id", "name"), "-")<br/>
그리고 결국, [HttpPost] 컨트롤러의 인덱스 :
[HttpPost]
public ActionResult Index(MyViewModel mvm) {
/* Here, how could I get the "Something" object that
was selected in the dropdownlist and can be identified
by the "id" property ?
*/
}
나는 완전히 틀렸다? 감사
'mvm.ListOfSomething.Where (o => o.Id == DropDownList.Selected.ID)'와 같은 것 같지만 dropdownlist.selected.id 부분은 확실하지 않습니다 .... – hellyale