-2

I have the following html.beginform. I want to make all of editorfor statement in foreach. Can someone help me? I use a ViewData.ModelMetadata.Properties, but the Save is out of function.

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

        <div class="form-horizontal">
            <h4>Test mit DbFunction</h4>
            <hr />
           @Html.ValidationSummary(true, "", new { @class = "text-danger" })
            <div class="form-group">
                @Html.LabelFor(model => model.ANLAGEID, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.ANLAGEID, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.ANLAGEID, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.ANLAGENAME, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.ANLAGENAME, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.ANLAGENAME, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.ANLAGEART, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.ANLAGEART, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.ANLAGEART, "", new { @class = "text-danger" })
                </div>
            </div>
                        <div class="form-group">
                @Html.LabelFor(model => model.ANLAGENUMMER, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.ANLAGENUMMER, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.ANLAGENUMMER, "", new { @class = "text-danger" })
                </div>
            </div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Speichern" class="btn btn-default" />
</div>

I use too foreach loop, but this is not working. It is possible to use a @for loop to solve me problem? If yes, can anyone explain how. Hier ist the foreach

@foreach (var property in ViewData.ModelMetadata.Properties)
    {

        <div class="editor-line">
            <label>@(property.DisplayName ?? property.PropertyName)</label>
            @Html.Editor(property.PropertyName)
        </div>

    }

1 Answers1

0

@foreach (var item in Model) {

    <td>
        @Html.DisplayFor(modelItem => item.EvaluatedRole)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.QuestionName)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.EvaluatedBy)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Description)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Weightage)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.AnswerName)
    </td>
    <td>
        @Html.ActionLink("Edit", "Edit", new { questionid=item.QuestionID }) 
   </td>
    <td>
        <button class="btn-link btnDelete" @*onclick="ConfirmationAlert()"*@>Delete</button>
        @Html.ActionLink("Delete", "Delete", new { questionid = item.QuestionID }, new { id="lnkBtn" ,style="Display:none"})
    </td>
</tr>

}

rahul bs
  • 1
  • 1
  • While this code may answer the question, providing information on how and why it solves the problem improves its long-term value. – L_J Jul 20 '18 at 10:30