我无法正确获取由模型中的枚举值填充的视图。
我尝试从该线程中阅读以帮助我使用 htmlhelper,但没有占上风: 如何找到相应值的枚举名称并在 DisplayFor 中使用它?
public static class HtmlExtensions
{
public static IHtmlString DisplayEnumFor<TModel>(this HtmlHelper<TModel> html, Expression<Func<TModel, int>> ex, Type enumType)
{
var value = (int)ModelMetadata.FromLambdaExpression(ex, html.ViewData).Model;
string enumValue = Enum.GetName(enumType, value);
return new HtmlString(html.Encode(enumValue));
}
}
但很可惜,它不起作用。我收到这条消息:
CS0266: Cannot implicitly convert type 'HiltiHaninge.Models.Discplines' to 'int'. An explicit conversion exists (are you missing a cast?)
我已尽最大努力尝试不使其成为 int,而是模型包含的该值的字符串表示形式。风景:
@model HiltiHaninge.Models.CellEntity
@using HiltiHaninge.Helpers
@{
ViewBag.Title = "Ta bort pass " + @Html.DisplayFor(model => model.Id) + ", på " + @Html.DisplayFor(model => model.Day);
}
<h2>@ViewBag.Title</h2>
<h3>Är du säker på att du vill radera inlägget?</h3>
<fieldset>
<legend>Pass @Html.DisplayFor(model => model.Id)</legend>
<div class="display-label">
<p class="text_without_indent">Tid från: (TT-MM)</p>
</div>
<div class="display-field">
@Html.DisplayFor(model => model.TimeFromHour)
@Html.DisplayFor(model => model.TimeFromMinute)
</div>
<div class="display-label">
<p class="text_without_indent">Tid till: (TT-MM)</p>
</div>
<div class="display-field">
@Html.DisplayFor(model => model.TimeToHour)
@Html.DisplayFor(model => model.TimeToMinute)
</div>
<div class="display-label">
<p class="text_without_indent">@Html.DisplayNameFor(model => model.Discipline)</p>
</div>
<div class="display-field">
@Html.DisplayEnumFor(model => model.Discipline, typeof(HiltiHaninge.Models.Discplines))
</div>
<div class="display-label">
<p class="text_without_indent">@Html.DisplayNameFor(model => model.Level)</p>
</div>
<div class="display-field">
@Html.DisplayEnumFor(model => model.Level, typeof(HiltiHaninge.Models.Levels))
</div>
<div class="display-label">
<p class="text_without_indent">@Html.DisplayNameFor(model => model.Coach)</p>
</div>
<div class="display-field">
@Html.DisplayFor(model => model.Coach)
</div>
</fieldset>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
<p>
<input type="submit" value="Ta bort" /> |
@Html.ActionLink("Tillbaka till listan", "Index")
</p>
}
任何人有任何想法,我将不胜感激。