너무 복잡하여 부담 스럽습니다.ViewData를 통해 SelectList를 편집기 템플릿으로 전달 - 제대로 표시되지 않음
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Myapp.Models.Node>" %>
<% if (ViewData.TemplateInfo.TemplateDepth > 1)
{ %>
<%= ViewData.ModelMetadata.SimpleDisplayText %>
<% }
else
{ %>
<table cellpadding="0" cellspacing="0" border="0">
<% foreach (var prop in ViewData.ModelMetadata.Properties.Where(pm => pm.ShowForEdit && !ViewData.TemplateInfo.Visited(pm)))
{ %>
<% if (prop.HideSurroundingHtml)
{ %>
<%= Html.Editor(prop.PropertyName) %>
<% }
else
{ %>
<tr>
<td>
<div class="editor-label" style="text-align: right;">
<%= prop.IsRequired ? "*" : ""%>
<%= Html.Label(prop.PropertyName)%>
</div>
</td>
<td>
<div class="editor-field">
<% if (ViewData.Keys.Contains(prop.PropertyName))
{
if ((ViewData[prop.PropertyName]).GetType().Name == "SelectList")
{ %>
<%= Html.DropDownList(prop.PropertyName, (SelectList)ViewData[prop.PropertyName])%>
<% }
else
{ %>
<%= Html.Editor(prop.PropertyName)%>
<% } %>
<% }
else
{ %>
<%= Html.Editor(prop.PropertyName)%>
<% } %>
<%= Html.ValidationMessage(prop.PropertyName, "*")%>
</div>
</td>
</tr>
<% } %>
<% } %>
</table>
<% } %>
그럼, 템플릿이하는 것은 표시입니다 : 뷰는과 같이 템플릿입니다
Node nd = _repo.getNode(id);
List<Category> ac = new List<Category>();
ac.AddRange(_repo.getCategories());
SelectList acl = new SelectList(ac, "category_id", "category_name", ac.Where(cat => cat.category_id == nd.category_id).First());
ViewData["category_id"] = acl;
return View(nd);
:
의 내가 컨트롤러 편집 작업의 예를 가지고 다음과 같이 정의했습니다 가정 해 봅시다 ViewData["property_name"]
이있는 모든 속성의 드롭 다운 목록
또한 Node 클래스의 모든 속성에 대해 DisplayName
메타 데이터 속성을 정의했습니다.
자, 드롭 다운 목록 괜찮 표시하고 제대로 설치되고 있지만 :
목록에서 첫 번째 값은 항상 선택되어- 의 selectList의 선택된 값 술어 괜찮와 적절한 값을 설정 않더라도 (적어도 디버거에서).
- 템플릿의 Html.Label은 속성의 적절한 DisplayName을 반환하지만 드롭 다운 목록을 표시하도록 ViewData를 정의하면 레이블이 일반 속성 이름 (예 :
Category
대신category_id
)으로 재설정됩니다.
무엇을 제공합니까? 이 기능을 수행하기위한 "깔끔한"방법을 생각해 볼 수 있습니까?