편집기 템플릿의 일부로 드롭 다운을 표시하려면 어떻게합니까?동적 드롭 다운이있는 MVC 3 편집기 템플릿
그래서 Users 엔티티와 Roles 엔티티가 있습니다. 역할은보기로 선택 목록으로 전달되고 사용자는 물론 사용자로 전달됩니다. SelectList는 올바른 ID가 선택되고 모든 것이 thanks to this sample 인 드롭 다운 메뉴가됩니다.
MVC 3을 사용하여 내 엔티티에 대한 올인원 멋지게 번들 된 EditorTemplate을 얻으 려하므로 EditorForModel을 호출하고 외래 키가있을 때마다 드롭 다운을 사용하여 필드를 멋지게 배치 할 수 있습니다. 이 특별한 경우에 역할과 같은 것들을 위해.
내 EditorTemlates \ User.cshtml (동적을 ViewData에 따라 레이아웃을 생성) : 다음 별도로 selectList의 바인딩있어보기에
<table style="width: 100%;">
@{
int i = 0;
int numOfColumns = 3;
foreach (var prop in ViewData.ModelMetadata.Properties
.Where(pm => pm.ShowForDisplay && !ViewData.TemplateInfo.Visited(pm)))
{
if (prop.HideSurroundingHtml)
{
@Html.Display(prop.PropertyName)
}
else
{
if (i % numOfColumns == 0)
{
@Html.Raw("<tr>");
}
<td class="editor-label">
@Html.Label(prop.PropertyName)
</td>
<td class="editor-field">
@Html.Editor(prop.PropertyName)
<span class="error">@Html.ValidationMessage(prop.PropertyName,"*")</span>
</td>
if (i % numOfColumns == numOfColumns - 1)
{
@Html.Raw("</tr>");
}
i++;
}
}
}
</table>
을, 나는 템플릿의 일부로 수행 할 .
내 모델 :
public class SecurityEditModel
{
[ScaffoldColumn(false)]
public SelectList roleList { get; set; }
public User currentUser { get; set; }
}
내 컨트롤러 :
public ViewResult Edit(int id)
{
User user = repository.Users.FirstOrDefault(c => c.ID == id);
var viewModel = new SecurityEditModel
{
currentUser = user,
roleList = new SelectList(repository.Roles.Where(r => r.Enabled == true).ToList(), "ID", "RoleName")
};
return View(viewModel);
}
내보기 :
@model Nina.WebUI.Models.SecurityEditModel
@{
ViewBag.Title = "Edit";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Edit</h2>
@using(Html.BeginForm("Edit", "Security"))
{
@Html.EditorFor(m => m.currentUser)
<table style="width: 100%;">
<tr>
<td class="editor-label">
User Role:
</td>
<td class="editor-field">
<!-- I want to move this to the EditorTemplate -->
@Html.DropDownListFor(model => model.currentUser.RoleID, Model.roleList)
</td>
</tr>
</table>
<div class="editor-row">
<div class="editor-label">
</div>
<div class="editor-field">
</div>
</div>
<div class="editor-row"> </div>
<div style="text-align: center;">
<input type="submit" value="Save"/>
<input type="button" value="Cancel" onclick="location.href='@Url.Action("List", "Clients")'"/>
</div>
}
는 희망이 충분히 분명, 더 많은 설명을 사용할 수 있는지 알려주세요. 미리 감사드립니다!
이것을 정리 했습니까? – REMESQ
대부분, 내가 찾던 해결책이 아니지만 벤의 대답은 믿을 것입니다. –