当前位置 : 主页 > 编程语言 > c语言 >

vb.net – 生成格式化Checkbox表的Mvc3 HtmlHelper方法

来源:互联网 收集:自由互联 发布时间:2021-06-24
这是vb.net中的mvc3 Razor项目.我需要创建一个动态表,1)可以包含1列到3列和1到多行…表中的项是复选框..我已经在下面的类中有工作的帮助方法但是下面的方法因为它只是返回字符串以及
这是vb.net中的mvc3 Razor项目.我需要创建一个动态表,1)可以包含1列到3列和1到多行…表中的项是复选框..我已经在下面的类中有工作的帮助方法但是下面的方法因为它只是返回字符串以及Ntd函数的实际字符串,因此只是为了了解应该在那一点发生什么…我迷失了,因为它产生了这些复选框可以绑定,所以控制器post方法将保存…如果我只是转储页面上的所有复选框,他们将保存和更新正确.布局只是一个眼睛疼痛..

这是当前的观点

@ModelType xxxxxx.CourseModel

@Code
    ViewData("Title") = "Edit Courses"
End Code

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"  type="text/javascript"></script>

@Using Html.BeginForm(Nothing, Nothing, FormMethod.Post, New With {.enctype = "multipart/form-data"})
    @Html.ValidationSummary(True)

    @<fieldset>
        <legend>Edit Courses</legend>
        @Html.HiddenFor(Function(model) model.cId)
        <table style="float: left">
            <tr>
                <th>Certification Bodies</th>
            </tr>
            <tr>
                @For _i As Integer = 0 To Model.Bodies.Count - 1
                    Dim i = _i
                    @<td>
                        @Html.CheckBoxFor(Function(model) model.Bodies(i).certSelected)
                        @Html.DisplayFor(Function(f) f.Bodies(i).certName)
                        @Html.HiddenFor(Function(model) model.Bodies(i).certBodyId)
                    </td>
                Next
            </tr>
            <tr>
                <th><input type="submit" value="Save" /></th>
            </tr>
        </table>
    </fieldset>
end using

这是辅助方法

<Extension()> _
Public Function CreateCheckBoxTable(ByVal helper As HtmlHelper, ByVal d As List(Of CertBodyVM)) As MvcHtmlString
    Dim htmlDisplayer As String = Table()
    Dim counter As Integer = 0
    For Each item In d
        If counter = 0 Then
            htmlDisplayer = htmlDisplayer + NRow()
        End If
        counter += 1
        If Not counter >= 3 Then
            htmlDisplayer = htmlDisplayer + Ntd("@Html.CheckBoxFor(Function(model) model.Bodies(i).certSelected)@Html.DisplayFor(Function(f) f.Bodies(i).certName)@Html.HiddenFor(Function(model) model.Bodies(i).certBodyId)")
        Else
            counter = 0
            htmlDisplayer = htmlDisplayer + CRow()
        End If
    Next
    htmlDisplayer = htmlDisplayer + CTable()
    Dim x As MvcHtmlString = MvcHtmlString.Create(htmlDisplayer)
    Return x
End Function

Public Function Table() As String
    Return String.Format("<table>")
End Function
Public Function CTable() As String
    Return String.Format("</table>")
End Function
Public Function NRow() As String
    Return String.Format("<tr>")
End Function

Public Function TdEnd() As String
    Return String.Format("</td>")
End Function

Public Function CRow() As String
    Return String.Format("</tr>")
End Function

Public Function Ntd(ByVal text As String) As String
    Return String.Format("<td>{0}</td>", text)
End Function

要调用帮助器方法,我只是计划替换每个循环及其内容

@Html.CreateCheckBoxTable(Model.Bodies)

这个方法正在生成具有正确行和列的相应表,但我在复选框上丢失了..

下面是正在生成的当前输出..

<tr><td><table><tr><td>@Html.CheckBoxFor(Function(model) model.Bodies(i).certSelected)@Html.DisplayFor(Function(f) f.Bodies(i).certName)@Html.HiddenFor(Function(model) model.Bodies(i).certBodyId)</td></table></td></tr>
你的代码不起作用.您将不得不重新组合助手中的lambda表达式.另外我建议你让这个CreateCheckBoxTable助手使用lambda表达式而不是只有一个CertBodyVM列表,这样你可能想在这个表中使用的CheckBox(或任何其他输入元素)将具有正确的名称:

Public Module MyModule
    Private indexerMethod As MethodInfo = GetType(IList(Of CertBodyVM)).GetMethod("get_Item")
    Private certSelectedProperty As PropertyInfo = GetType(CertBodyVM).GetProperty("CertSelected")
    Private certNameProperty As PropertyInfo = GetType(CertBodyVM).GetProperty("CertName")
    Private certBodyIdProperty As PropertyInfo = GetType(CertBodyVM).GetProperty("CertBodyId")

    <Extension()> _
    Public Function CreateCheckBoxTable(Of TModel)(ByVal helper As HtmlHelper(Of TModel), ByVal ex As Expression(Of Func(Of TModel, IList(Of CertBodyVM)))) As IHtmlString
        Dim table = New TagBuilder("table")
        Dim metadata = ModelMetadata.FromLambdaExpression(ex, helper.ViewData)
        Dim bodies = CType(metadata.Model, List(Of CertBodyVM))
        Dim tableBody = New StringBuilder()
        For i = 0 To bodies.Count - 1
            Dim ex1 = MakePropertyExpression(Of TModel, Boolean)(ex, certSelectedProperty, i)
            Dim ex2 = MakePropertyExpression(Of TModel, String)(ex, certNameProperty, i)
            Dim ex3 = MakePropertyExpression(Of TModel, Integer)(ex, certBodyIdProperty, i)

            Dim tr = New TagBuilder("tr")
            Dim td = New TagBuilder("td")
            td.InnerHtml = String.Concat(
                helper.CheckBoxFor(ex1),
                helper.DisplayFor(ex2),
                helper.HiddenFor(ex3)
            )

            tr.InnerHtml = td.ToString()
            tableBody.Append(tr.ToString())
        Next
        table.InnerHtml = tableBody.ToString()

        Return New HtmlString(table.ToString())
    End Function

    Private Function MakePropertyExpression(Of TModel, TProperty)(ByRef ex As Expression(Of Func(Of TModel, IList(Of CertBodyVM))), ByRef pi As PropertyInfo, ByVal i As Integer) As Expression(Of Func(Of TModel, TProperty))
        Return Expression.Lambda(Of Func(Of TModel, TProperty))(
            Expression.Property(
                Expression.Call(ex.Body, indexerMethod, Expression.Constant(i)),
                pi
            ),
            ex.Parameters()
        )
    End Function
End Module

然后你可以像这样使用它:

@ModelType xxxxxx.CourseModel
...
@Html.CreateCheckBoxTable(Function(x) x.Bodies)
网友评论