开发者

Saving the value of a Selectlist item when used in a for each loop in a view

Part of my MVC 3 VB.NET has a view that uses a list(of model) and a for each on that model to list all the contents.. The problem is that a selectlist is part of each item.. When the value is selected for each item it is not being posted back to the controller with the rest of the items... The view code is as follows:

@ModelTYPE List(Of xxxxxxx.attendance)


@Code
ViewData("Title") = "Class Attendance Record"
End Code


@Using Html.BeginForm
@<fieldset>

<table>
<tr>
    <th>
        First Name
    </th>
    <th>
        Last Name
    </th>
    <th>
        Registrant ID
    </th>
    <th>
        Course Status
    </th>
    <th>
        Comments
    </th>

</tr>

@For r As Integer = 0 To Model.Count - 1
    Dim i As Integer = r
    @Html.HiddenFor(Function(m) m(i).id)
@<tr>

    <td>
        @Html.DisplayFor(Function(m) m(i).firstName)
        @Html.HiddenFor(Function(m) m(i).firstName)
    </td>.

    <td>
        @Html.DisplayFor(Function(m) m(i).lastName)
         @Html.HiddenFor(Function(m) m(i).lastName)
    </td>
    <td>
        @Html.DisplayFor(Function(m) m(i).reg_id)
         @Html.HiddenFor(Function(m) m(i).reg_id)
    </td>
    <td>
        @Html.DisplayFor(Function(m) m(i).Completed_Class)

        </td>
    <td>
        @Html.DropDownList("Completed_Class", New SelectList(ViewBag.courseStatus, "Status", "Status"), New With {.Completed_Class = "Completed_Class"})
        @Html.HiddenFor(Function(m) m(i).Completed_Class)
    </td>
    <td>
        @Html.TextBoxFor开发者_开发知识库(Function(m) m(i).Comments, New With {.class = "AttenComment"})
         @Html.HiddenFor(Function(m) m(i).Comments)
   </td>
 </tr>
 Next
 </table>
 <p>
     <input type="submit" name="submit" />
    </p>
  </fieldset>

  End Using

Any ideas how I can assign the selectedvalue to the HiddenFor??


Instead of writing loops, I would recommend you using editor templates. Like this:

@ModelTYPE List(Of xxxxxxx.attendance)

@Code
    ViewData("Title") = "Class Attendance Record"
End Code

@Using Html.BeginForm
    @<fieldset>
        <table>
            <tr>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Registrant ID</th>
                <th>Course Status</th>
                <th>Comments</th>
            </tr>
            @Html.EditorForModel()
        </table>
        <p><input type="submit" name="submit" /></p>
    </fieldset>
End Using

and inside the corresponding editor template (~/Views/Shared/EditorTemplates/attendance.vbhtml or ~/Views/SomeController/EditorTemplates/attendance.vbhtml) which will be rendered for each element of the collection:

@ModelTYPE xxxxxxx.attendance
@Html.HiddenFor(Function(m) m.id)
@<tr>
    <td>
        @Html.DisplayFor(Function(m) m.firstName)
        @Html.HiddenFor(Function(m) m.firstName)
    </td>
    <td>
        @Html.DisplayFor(Function(m) m.lastName)
        @Html.HiddenFor(Function(m) m.lastName)
    </td>
    <td>
        @Html.DisplayFor(Function(m) m.reg_id)
        @Html.HiddenFor(Function(m) m.reg_id)
    </td>
    <td>
        @Html.DisplayFor(Function(m) m.Completed_Class)
    </td>
    <td>
        @Html.DropDownList("Completed_Class", New SelectList(ViewBag.courseStatus, "Status", "Status"), New With {.Completed_Class = "Completed_Class"})
        @Html.HiddenFor(Function(m) m.Completed_Class)
    </td>
    <td>
        @Html.TextBoxFor(Function(m) m.Comments, New With {.class = "AttenComment"})
        @Html.HiddenFor(Function(m) m.Comments)
    </td>
</tr>

This being said, using all those hidden fields reminds me badly for the classic WebForms ViewState. You don't need it. Believe me, you really don't need this stuff. Use input fields if you allow the user to modify something (text fields, radios, checkboxes, etc...), but hidden fields, come on! A single hidden field containing the id of the model will largely suffice for the controller action to fetch the corresponding model from the datastore that it initially fetched it from.

As far as your dropdown is concerned:

@Html.DropDownList("Completed_Class", New SelectList(ViewBag.courseStatus, "Status", "Status"), New With {.Completed_Class = "Completed_Class"})

You should use a property on your view model to bind to and use the strongly typed version:

@Html.DropDownListFor(Function(m) m.CompletedCLass, New SelectList(ViewBag.courseStatus, "Status", "Status"), New With {.Completed_Class = "Completed_Class"})
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜