Displaying entities data with EditorForModel

2019-08-28 08:18发布

I've got data filled enumerator which I send to the view, I render the EditorForModel like this:

@model IEnumerable<TestProj.Models.Hours>
@Html.EditorForModel()

than in the EditorTemplates.Hours.cshtml:

@model TestProj.Models.Hours
<div>
@Html.CheckBoxFor(
    x => x.Selected, 
    new { 
        @class = "jcf-unselectable",
        id = HtmlHelper.GenerateIdFromName("cb_slice." + ViewData.TemplateInfo.GetFullHtmlFieldName(""))
    }
)
@Html.LabelFor(x => x.SliceName)
</div>

When the page is rendered I want to show all the the SliceNames data , Not the entity's name "SliceName" for the labels display.

So it will render the checkbox along with the label with the data behind SliceName.

I'm trying this option since I've understood using viewbags with loops ain't recommended, However I'd like suggestion behind the EditorForModel scope as well.

EDIT: If I use :

@Html.DisplayTextFor(x => x.SliceName)

-Ill get the data behind SliceName (Text only).

If I Use :

@Html.LabelFor(x => x.SliceName)

-Ill get the title "SliceName" for the label's text.

How do I use @Html.LabelFor to get the data as text like @Html.DisplayTextFor?

1条回答
迷人小祖宗
2楼-- · 2019-08-28 09:16

It sounds like you may have a problem with your Form.

If you do:

@Html.BeginForm()
{
...
}

instead of this:

@using(Html.BeginForm())
{
...
}

The names of your Model will appear instead of the values of the Model.

查看更多
登录 后发表回答