Kenci
Kenci

Reputation: 4882

Why doesnt EditorFor render HTML?

I have the following ViewModel:

public class ShowMatrixQuestionViewModel : ShowQuestionViewModel
{

public Dictionary<MatrixRows, List<MatrixColumns>> columnrow;
public List<MatrixColumns> columns;
public List<MatrixRows> rows;

public ShowMatrixQuestionViewModel()
{
    columns = new List<MatrixColumns>();
    rows = new List<MatrixRows>();
    columnrow = new Dictionary<MatrixRows, List<MatrixColumns>>();
}
}

public class MatrixColumns
{
    public int Column_ID { get; set; }
    public int Column_Number { get; set; }
    public String Column_Description { get; set; }
    public Boolean IsAnswer { get; set; }
}

public class MatrixRows
{
    public int Row_Id { get; set; }
    public String Row_Number { get; set; }
    public String Row_Description { get; set; }
}

When i fill up my model and try to show the MatrixRows with a DisplayTemplate it doesnt show anything. The name of my DisplayTemplate is MatrixRows.cshtml and it is placed inside Views/Shared/DisplayTemplates

This is the code i use to display a MatrixRow:

@model TestInheritance.Models.ShowMatrixQuestionViewModel

@using (Html.BeginForm())
{
    <div id="edit">
    <h2>Columns</h2>
    @foreach (var column in Model.columns)
    {
        <p>@column.GetType().Name</p>
        Html.RenderPartial("EditMatrixColumn", column);
    }
    <h2>Rows</h2>
    @foreach (var rows in Model.rows)
    {
      <p>@rows.GetType()</p>
      Html.DisplayFor(x => rows);
      //Html.RenderPartial("EditMatrixRow", rows);
    }
    </div>

    <input type="submit" value="Finished" />
}

It works fine when i used RenderPartial... What am i doing wrong?

The code for the DisplayTemplate:

@model TestInheritance.Models.MatrixRows
@using TestInheritance.Helpers

<div class="editrow">
@using (Html.BeginCollectionItem("rows")) 
{
    <span>
    Nummer:
    @Html.EditorFor(cn => Model.Row_Number)
    </span>
    <br />
    <span>
    Beskrivelse:
    @Html.EditorFor(bs => Model.Row_Description)
    </span>
}
</div>

Upvotes: 0

Views: 1445

Answers (2)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039538

Instead of writing loops:

@foreach (var rows in Model.rows)
{
    <p>@rows.GetType()</p>
    Html.DisplayFor(x => rows);
}

simply:

@Html.DisplayFor(x => x.rows)

and then inside the corresponding display template ~/Views/Shared/DisplayTemplates/MatrixRows.cshtml which will automatically be rendered for each element of the collection:

@using TestInheritance.Helpers
@model TestInheritance.Models.MatrixRows

<p>@GetType()</p>
<div class="editrow">
@using (Html.BeginCollectionItem("rows")) 
{
    <span>
        Nummer:
        @Html.EditorFor(x => x.Row_Number)
    </span>
    <br />
    <span>
        Beskrivelse:
        @Html.EditorFor(x => x.Row_Description)
    </span>
}
</div>

This being said, your display template looks more like an editor template as it contains input fields. So:

@Html.EditorFor(x => x.rows)

and then use ~/Views/Shared/EditorTemplates/MatrixRows.cshtml

Upvotes: 2

SLaks
SLaks

Reputation: 888283

Html.DisplayFor returns HTML.
You aren't doing anything with that HTML.

You probably want to write it to the page by writing @Html.DisplayFor(...)

Upvotes: 5

Related Questions