1

我正在尝试遍历数据库并为每条记录创建一个文本框。在我尝试使用控制器中的信息之前,一切正常。如何将此信息传递回控制器?

<% Using (Html.BeginForm("NewEnvCd", "Config", Nothing, FormMethod.Post))%>

    <%: Html.Hidden("Environment", Model.NewEnvironment)%>
    <div class="box" style="margin-top:30px;width:800px;">
    <h4 >Add New Environment Code</h4>
        <div>
        <div>
        <div style="float:left; text-decoration:underline;">Environment Code</div><div style="float:right;width:400px;text-decoration:underline;">Parameter Values</div>
        </div>
        <br /><br />
        <% Dim i As Int32 = 0 %>
        <% For Each cfg In Model.ParameterValues%>

            <div style="margin-bottom:5px;">

                <div style="float:left;">
                    <%: Html.Hidden("newEnvironment", Model.NewEnvironment)%>
                    <%: Html.Hidden("cfg[" & i & "].Name", cfg.Name) %>
                    <%: Html.Hidden("cfg[" & i & "].EnvCd", cfg.EnvCd) %>
                    <%: cfg.Name%>

                </div>
                <div style="float:right;">
                    <%: Html.TextBox("cfg[" & i & "]value", If(Not cfg.Value Is Nothing, cfg.Value, ""), New With {.style = "width:400px;"})%>
                </div>
                <div class="clear"></div>
            </div>
            <% i += 1 %>

        <% Next %>

        <input type="submit" value="Save" />
        <%: Html.ActionLink("Return to List", "Index", "Config") %>
        </div>
        </div>
<% End Using %>

控制器如下所示:

    Function NewEnvCd(ByVal newEnvironment As String, Optional ByVal copyEnvironment As String = "") As ActionResult
        Dim model As NewEnvCdModel
        model = New NewEnvCdModel(newEnvironment, copyEnvironment)
        Return View(model)
    End Function

    <HttpPost()>
    Function NewEnvCd(ByVal newEnvironment As String, ByVal name As String, ByVal value As String, ByVal cfg As IList(Of ConfigData.tblTT_Configuration))

        If (cfg.Count() = 0 OrElse String.IsNullOrWhiteSpace(name)) Then
            Return RedirectToAction("Index")
        End If

        For Each c In cfg
            Repository.CreateEnvironment(If(c.EnvCd, newEnvironment), c.Name, c.Value)
        Next
        Return RedirectToAction("index")
    End Function

我可能不清楚我的问题,但我正在寻找的答案与使用每个 cfg 记录的名称有关(即 cfg.Name、cfg.Value、cfg.EnvCd)。我试图单独传递 cfg 的参数(ByVal 名称作为字符串,ByVal 值作为字符串),当我应该将 cfg 作为列表或可查询(ByVal cfg As IList(Of ConfigData.tblTT_Configuration))传递时,然后从控制器中提取值。谢谢你们的帮助。

4

1 回答 1

1

假设您的ConfigData.tblTT_Configuration模型具有名为 的属性NameEnvCd并且Value您需要做的就是修复您的 TextBox 名称,以便naming convention在绑定到列表时遵守:

<%: Html.TextBox(
    "cfg[" & i & "].Value", 
    If(Not cfg.Value Is Nothing, cfg.Value, ""), 
    New With {.style = "width:400px;"}
) %>

请注意,您在值之前缺少一个点。

于 2013-02-01T07:25:56.240 回答