2011-08-18 3 views
0

아래 코드에서 model.Title은 적절한 선택 값을 보유하고 있지만 id는 'title'인 ddl에 설정되지 않습니다.MVC3.0의 HTML.Dropdownlistfor에서 선택된 값을 채우는 다른 방법

다른 방법으로 선택한 값을 설정할 수 있습니까? (문서 등의 준비?) 내 컨트롤러에서

<td> 
@Html.DropDownListFor(model => model.Title, Model.TitleList, !Model.IsTitleEditable ? (object)new { id = "Title", @disabled = "disabled", @style = "width:250px;" } : (object)new { id = "Title", @style = "width:250px" }) 
</td> 

selectList의 아래로 채워지고 : 나는 다른 오버로드 방법을 사용하고 있습니다이 경우

model.TitleList = new SelectList(GetAllTitles(), "Code","Value"); 

어떻게 selectedValue 속성을 설정합니다 이 selectList?

+0

model.TitleList 및 model.Title을 설정하는 코드를 설정하는 코드를 표시 할 수 있습니까? –

답변

1

Model.TitleListSelectList 인 경우 모델 생성 중에 SelectList을 채울 때 선택한 값을 지정할 수 있습니다. 예를 들면 다음과 같습니다.

var model = new MyViewModel(); 
var domainEntity = GetMyDomainEntity(id); 

// Create a selectlist using all your titles and specifying the Title value of the item 
// you're viewing as the selected item. See parameter options if you're not supplying 
// an object as the selected item value 
model.TitleList = new SelectList(GetAllTitles(), domainEntity.Title) 

그러면 내 생각에 Html.DropDownListFor 만 수행하면됩니다.

+0

그러나 내 컨트롤러에서 선택 목록은 아래와 같이 채워집니다. model.TitleList = new SelectList (GetAllTitles(), "Code", "Value"); 이 경우 다른 오버로드 된 메서드를 사용하여이 selectList의 selectedValue 속성을 설정하는 방법은 무엇입니까? – Biki

+0

모델로 채울 수 있습니다.TitleList = 새로운 SelectList (GetAllTitles(), "Code", "Value", domainEntity.Title)'. 기본적으로 선택한 항목 인 네 번째 매개 변수를 추가하십시오. –

3

당신은 당신이 모두 텍스트에게 SelectListItems model.Value가 SelectListItems의 값 중 하나의 값 세트를 IEnumerable <SelectListItem>로 model.TitleList을 만들 경우 다음 모든 것이 작동합니다 . 그래서 :

model.TitleList = GetAllTitles() 
      .ToList() 
      .Select(i => new SelectListItem { 
           Value = i.Id.ToString(), 
           Text = i.Description }); 

model.Title = 5; 

하고보기

: 아이디 = "제목"는 HtmlAttributes에 필요하지 않다고

<td> 
@Html.DropDownListFor(model => model.Title, 
         Model.TitleList, 
         !Model.IsTitleEditable 
         ? (object)new { @disabled = "disabled", @style = "width:250px;" } 
         : (object)new { @style = "width:250px" }) 
</td> 

주, 도우미는 당신을 위해 ID를 생성합니다.

편집SelectListItemSelected 부동산에 관한 약간의 혼동이있다. 이것은 이 아니며 DropDownListFor을 사용할 때 사용되는DropDownList에서만 사용됩니다. 따라서 DropDownListFor의 경우 모델의 속성을 선택한 값 (위에서 언급 한 model.Title = 5;)으로 설정합니다.

1

PERSONNALY 나는 정적 이와 같은 클래스 데 뭘하려는 : 내가 가진 경우,

@Html.DropDownListFor(x => x.Title, SelectLists.Titles(Model.Title), !Model.IsTitleEditable ? (object)new { id = "Title", @disabled = "disabled", @style = "width:250px;" } : (object)new { id = "Title", @style = "width:250px" }); 

내 모든 SelectLists 해당 클래스에 내 뷰에서

public static class SelectLists 
{ 
    public static IList<SelectListItem> Titles(int selected = -1) 
    { 
     return GetAllTitles() 
      .Select(x => new SelectListItem { 
        Value = x.Id.ToString(), 
        Text = x.Description, 
        Selected = x.Id == selected 
       }).ToList(); 
    } 
} 

그리고 다음을 그들 중 너무 많은 것들은 다른 클래스 안에서 분리 될 것입니다.

다른보기/동작에서 동일한 드롭 다운 목록이 필요하면 컨트롤러에서 코드를 반복 할 필요가 없으므로 유용하다는 것을 알았습니다.

관련 문제