2

下面的代码取自本教程的讲师编辑视图:http ://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/updating-related-data-with-the- asp-net-mvc 应用程序中的实体框架

    <div class="editor-field">
        <table style="width: 100%">
            <tr>
                @{
                    int cnt = 0;
                    List<ContosoUniversity.ViewModels.AssignedCourseData> courses = ViewBag.Courses;

                    foreach (var course in courses) {
                        if (cnt++ % 3 == 0) {
                            @:  </tr> <tr> 
                        }
                        @: <td> 
                            <input type="checkbox" 
                                   name="selectedCourses" 
                                   value="@course.CourseID" 
                                   @(Html.Raw(course.Assigned ? "checked=\"checked\"" : "")) /> 
                            @course.CourseID @:&nbsp; @course.Title
                        @:</td>
                    }
                    @: </tr>
                }
        </table>
    </div>

此代码呈现以下课程复选框:

编辑查看 http://i2.asp.net/media/2577999/Windows-Live-Writer_Updating-Re.NET-MVC-Application-6-of-10h_AEF7_Instructor_edit_page_with_courses_1.png

使用辅助方法和编辑器模板实现相同的结果会更优雅,对吧?例如:

看法

    <div class="editor-field">
        <table style="width: 100%">
            @Html.EditorFor(model => model.courses)
        </table>
    </div>

编辑器模板

@model AssignedCourseData
@using ContosoUniversity.ViewModels

<tr>
<td>
    @Html.HiddenFor(model => model.CourseID)    
    @Html.CheckBoxFor(model => model.Assigned)
    @Html.DisplayFor(model => model.CourseName)
</td>
</tr>

除了这不会将复选框呈现为 3 列表。有人可以建议怎么做吗?

4

0 回答 0