2014-01-20 2 views
1

안녕하세요, 내 dropDownList에 html 속성 데이터 바인딩을 추가하려고합니다. 여기@ html.dropdownlist에 HtmlAttributes 추가

당신은 드롭 다운 내 코드를 볼 수 있습니다

DeviceDictionaryId는 아래와 같이 ViewBag를 통해 컨트롤러에서 보내는입니다
@Html.DropDownList("DeviceDictionaryId" ,String.Empty) 

:

var Ids = db.Dictionaries 
      .Select(s => new 
      { 
       DeviceDictionaryId = s.DeviceDictionaryId, 
       Description = s.DeviceManufacturer + " " + s.DeviceName 
      }) 
      .ToList(); 
     ViewBag.DeviceDictionaryId = new SelectList(Ids, "DeviceDictionaryId", "Description"); 

하지만 난 방법을 제대로 Htmlattribute을 추가하는 솔루션을 찾을 수 없습니다 . "= DeviceDictionaryId optionLabel = String.Empty로 htmlattributes = 새로운 {data_bind :

public static MvcHtmlString DropDownList(
string name, 
IEnumerable<SelectListItem> selectList, 
string optionLabel, 
Object htmlAttributes 

) 나를 위해

:

이름

나는 내가 사 개 인수를 제공 할 필요가 있음을 알고 값 : Id "}

하지만 무엇을 IEnumerable<SelectListItem> selectList,으로 넣어야합니까?

1) 사용 jQuery를 아래처럼 속성을 추가 :이 오류를 무시하는 두 가지 방법을 발견

CS1973: 'System.Web.Mvc.HtmlHelper<dynamic>' has no applicable method named 'DropDownList' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax. 
+1

IEnumerable이므로,'Ids' 변수를 넣어야합니다 (제네릭 형식을'SelectListItem'으로 변경할 수 있습니다). 'DeviceDictionaryId'는'SelectList' 타입이므로 작동하지 않습니다. – Marthijn

답변

0

확인 :

것은하다면 나는이 오류가 내 ViewBag.DeviceDictionaryId을 넣어보십시오 :

$("#DeviceDictionaryId").attr("data-bind", "value:DeviceId"); 

2) 하거나 대신 IEnumerable을 null이 통과하고 모든 작동합니다

@Html.DropDownList("DeviceDictionaryId", null, new { data_bind = "value:DeviceId" }) 
+0

두 번째 것은 최선의 선택입니다. –

+0

이 옵션을 선택하면 목록의 동작이 변경됩니다. 첫 번째 목록 멤버는'htmlAttributes'가 없으므로 기본적으로 null 대신에 선택됩니다. – tr3v

관련 문제