0

MVC4의 드롭 다운 목록을 사용하여 편집기 템플릿을 만들려고합니다. 나는 dropdownlistfor 같은 뷰에서 직접 작업을 얻을 수 있습니다 :Html.DropDownListFor를 EditorTemplate으로 이동

@Html.DropDownListFor(model => model.Item.OwnerId, new SelectList(Model.DDLOptions.CustomerOptions, "Value", "DisplayText")) 

그러나 그것을 "generify"및 에디터 템플릿에 그것을 넣어, 나는 그것이 동작하지 않습니다.

@Html.DropDownListFor(model => model, new SelectList(Model.DDLOptions.CustomerOptions, "Value", "DisplayText")) 

나는 오류가 점점 오전 :

public class DDLOptions<T> 
{ 
    public T Value { get; set; } 
    public string DisplayText { get; set; } 
} 

이 작업을 수행합니다

Exception Details: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'int' does not contain a definition for 'DDLOptions' 

Model.DDLOptions.CustomerOptions 유형 IEnumerable<DDLOptions<int>>이다를 여기

나는 부분 내 EditorTemplate에 노력하고 무엇인가 오류가 일반적인 DDLOptions과 관련이 있습니까?

@Html.DropDownListFor(model => model, new SelectList(Model.DDLOptions.CustomerOptions, "Value", "DisplayText")) 

단순히 위의 코드를 기반 INT,하지만 그때 너무 부분에서 new SelectList(Model.DDLOptions.CustomerOptions, "Value", "DisplayText")를 호출하고 귀하의 모델을 참조 Model.DDLOptions하지 않습니다 :

답변

1

이 줄은 문제가 편집기 템플릿에서 모델에 존재해야합니다. 귀하의 모델은 단지 int입니다.

는 항목 소유자에 대한 사용자 정의 모델 클래스를 생성하고는 ownerID과 DDLOptions를 포함해야 그 중 하나이 작업을 수행하는 방법에는 여러 가지가 있습니다. 또 다른 것은 ViewBag에 DDLOptions을 고수하는 것이지만, 필자는 잘 작성된 뷰 특정 뷰 모델을 사용하는 것을 선호하므로 일반적으로 그렇게하지 않습니다.

이 정보가 도움이되기를 바랍니다.

+1

감사합니다. 당신이 그것을 전달하지 않으면 편집기 템플릿은 전체 뷰 모델을 인식하지 못합니다. @ Html.EditorFor (model => model.Item.Attribute1, new {Options = Model.DDLOptions.ItemAttributeOptions.Attribute1Options}) 편집기에 전달하는 방법은 다음과 같습니다. 여기에 데이터를 읽으려는 편집기가 있습니다. @Html.DropDownListFor (model => model, new SelectList (IEnumerable >) ViewData [ "Options"] "Value", "DisplayText", Model)) –