إنشاء قالب محرر object.cshtml باستخدام MVC وRazor
-
26-09-2019 - |
سؤال
إنني أتطلع إلى إنشاء قالب محرر لـ Object.cshtml لتغيير سلوك أسلوب Html.EditorForModel().لا يمكنني العثور على أي مثال على ذلك باستخدام Razor.لقد رأيت هذا المثال باستخدام محرك عرض MVC2 وWebForm ولكن لا تعرف ما يكفي عن ماكينة الحلاقة لتحويلها.حتى مثال بسيط سيكون مفيدًا جدًا.
المحلول
سأقوم فقط بعمل قالب العرض واترك الباقي كتمرين للقارئ :)
@if (Model == null) {
<text>@ViewData.ModelMetadata.NullDisplayText</text>
} else if (ViewData.TemplateInfo.TemplateDepth > 1) {
<text>@ViewData.ModelMetadata.SimpleDisplayText</text>
} else {
<table cellpadding="0" cellspacing="0" border="0">
@foreach (var prop in ViewData.ModelMetadata.Properties.Where(pm => pm.ShowForDisplay && !ViewData.TemplateInfo.Visited(pm))) {
if (prop.HideSurroundingHtml) {
<text>@Html.Display(prop.PropertyName)</text>
} else {
<tr>
<td>
<div class="display-label" style="text-align: right;">
@prop.GetDisplayName()
</div>
</td>
<td>
<div class="display-field">
@Html.Display(prop.PropertyName)
</div>
</td>
</tr>
}
}
</table>
}
نصائح أخرى
يبدو أن هذا يعمل من أجل قالب المحرر للحصول على bootstrap ، يرجى إعلامي بأي تحسينات
Object.cshtml
@if (Model == null)
{
<text>@ViewData.ModelMetadata.NullDisplayText</text>
}
else if (ViewData.TemplateInfo.TemplateDepth > 1)
{
<text>@ViewData.ModelMetadata.SimpleDisplayText</text>
}
else
{
foreach (var prop in ViewData.ModelMetadata.Properties.Where(pm => pm.ShowForDisplay && !ViewData.TemplateInfo.Visited(pm)))
{
if (prop.HideSurroundingHtml)
{
<text>@Html.Editor(prop.PropertyName)</text>
}
else
{
<div class="form-group">
@Html.Label(prop.PropertyName, new { @class = "control-label col-md-2", @style = "text-align:right;" })
<div class="col-md-10">
@Html.Editor(prop.PropertyName, null, new { @class = "form-control " })
@Html.ValidationMessage(prop.PropertyName, "", new { @class = "text-danger" })
</div>
</div>
}
}
}
لا تنتمي إلى StackOverflow