2011-11-11 3 views

답변

0

내가 당신이 원하는 것을 얻을 100 % 확실하지 않다,하지만 난 당신이 선택한 가치를 얻을한다고 가정 드롭 다운 목록에서? 이 경우

: 나는 당신이 값 여기

+0

문제는 값이 드롭 다운에서 선택되고 post 메소드가 호출되어 선택한 값이 손실됩니다. – user1019480

1

수행 할 작업을 모르기 때문에

new { onchange = "alert(this.options[this.selectedIndex].value);" } 

내가있다, 지금 경고 한 예를 넣어, 내가 사용합니다. 나는 확실하지 않다, 이것은 당신이 또 다른 방법은

<%=Html.DropDownList("ddlCategories", IEnumerable<SelectListItem>)ViewData["PEDropDown"], "CategoryId", "CategoryName", Model.CategoryId), "Select Category", new { onchange = "this.form.action='/Screener/Screener';this.form.submit();"})%> 

,

List<SelectListItem> CategoryList = new List<SelectListItem>(); 
       foreach (var item in Categories) 
       { 

        CategoryList.Add(new SelectListItem 
        { 
         Selected = Model.CategoryId, 
         Text = item.CategoryName, Value = Convert.ToString(item.CategoryId) }); 
       } 
ViewData["PEDropDown"]=CategoryList; 

을 다음과 같이 컨트롤러 선택 목록을 만들고

<%:Html.DropDownList("ddlCategories",IEnumerable<SelectListItem>)ViewData["PEDropDown"], "CategoryId", "CategoryName", new { onchange = "this.form.action='/Screener/Screener';this.form.submit();"})%> 
로보기에서 사용

드롭 다운리스트를 채우는 데 사용하는 방법입니다
0

다시 컨트롤러에 값을 전달한 다음 컨트롤러에서 SelectListItems 목록을 채 웁니다.

public actionresult yourmethod (int idToPass) 
{ 

List<SelectListItem> SLIList = new List<SelectListItem>(); 
foreach (Model model in dropdownList) 
{ 
    SelectListItem SLI = new SelectListItem(); 
    SLI.text = model.CategoryName; 
    SLI.selected = model.CategoryId == idToPass; 
    SLIList.Add(SLI); 
} 
    ViewData["myDDL"] = SLIList; 
} 
0

시도해보십시오. (나는 모델 객체를 사용하는 것이 더 좋습니다, 제안)

Html.DropDownList("PEDropDown", new SelectList(ViewBag.PEDropDown, "Key", "Value", Model.PEDropDownSelectedValue), new { onchange = "document.location.href = '/ControllerName/ActionMethod?selectedValue=' + this.options[this.selectedIndex].value;" })) 

selectList의의 네 번째 인수는 선택된 값 대신을 ViewData의 ViewBag를 사용 . 모델 객체를 사용하여 전달해야합니다. 특정 작업 메서드를 호출 할 때 아래와 같이 모델 개체를 설정하십시오.

public ActionResult ActionMethod(string selectedValue) 
{ 
    ViewModelPE objModel = new ViewModelPE(); 
    // populate the dropdown, since you lost the list in Viewbag 
    ViewBag.PEDropDown = functionReturningListPEDropDown(); 
    objModel.PEDropDownSelectedValue = selectedValue; 
    return View(objModel); 
    // You may use the model object to pass the list too instead of ViewBag (ViewData in your case) 
} 
관련 문제