2012-01-30 2 views
0

면도기 및 MVC3을 사용하여 ViewModel을 렌더링하려고합니다. 어떤 이유에서든 내 SelectList (아래 WidgetTypes)는 나머지는 모두 정상적으로 렌더링되지만 렌더링하지 않습니다. 누군가가 왜 빛을 비추 줄 수 있습니까? 같은 문제가 발생할 위젯에 대한 동적 모델을 사용하기 때문에 각 필드에 대해 @ Html.EditorFor (m => m.SystemName) 등을 사용하도록보기를 변경하고 싶지 않습니다.@ Html.EditorFor를 사용할 때 MVC3이 SelectList를 렌더링하지 않습니다

내 뷰 모델 :

public class CreateWidgetViewModel 
{ 
    [Required] 
    public string Title { get; set; } 

    [Required(ErrorMessage = "The System Name is required")] 
    [Display(Name = "System Name")] 
    public string SystemName { get; set; } 

    [Required] 
    [Display(Name = "Widget Type")] 
    public string WidgetType { get; set; } 

    [Required] 
    [Display(Name = "Widget Types")] 
    public SelectList WidgetTypes { get; set; } 
} 

내 컨트롤러 :

[HttpGet] 
public ActionResult Create() 
{ 
    var widgetTypes = from wt in _widgetService.WidgetTypes 
         select new 
         { 
          Name = wt.WidgetName, 
          WidgetType = wt.GetType().AssemblyQualifiedName 
         }; 

    var viewModel = new CreateWidgetViewModel 
    { 
     WidgetTypes = new SelectList(widgetTypes, "WidgetType", "Name") 
    }; 

    if (Request.IsAjaxRequest()) 
    { 
     return PartialView(viewModel); 
    } 
    return View(viewModel); 
} 

내보기 : 당신은 당신이 드롭 다운 목록을 생성 할 경우 Html.DropdownListFor 도우미를 사용할 필요가

@{ 
    ViewBag.CurrentPage = "widgets"; 
} 
<h2>@ViewBag.Title</h2> 
@using (Html.BeginForm()) 
{ 
    @Html.ValidationSummary(true) 
    <fieldset> 
     <div> 
      @Html.EditorFor(model => model) 
     </div> 
    </fieldset> 
    <div> 
     <input type="submit" value="Save" />&nbsp;or&nbsp;@Ajax.ActionLink("Back to list", "Index", "Widget", 
     new AjaxOptions { HttpMethod = "Get", UpdateTargetId = "ajax-replace" }) 
    </div> 
} 
+0

모델에서 SelectList를 사용하면 직렬화 할 수 없으므로 좋지 않습니다. 상태 서버를 켜면 토스트가됩니다. – gangelo

답변

2

. SelectList을 일부 속성의 유형으로 사용했다고해서 기본 편집기 템플릿이 <select> 상자를 렌더링하지는 않습니다. 따라서 사용자 정의 편집기 템플리트를 작성해야합니다.

이러한 기본 템플릿이 어떻게 구현되는지 보려면 blog post을 살펴보십시오.

+0

나는 그것을 지금했다. 이런 식으로 행복하지 않지만 그냥 감사해야합니다. – Pieter

관련 문제