html.textbox命名为asp.net mvc

时间:2013-01-31 23:02:18

标签: asp.net-mvc vb.net asp.net-mvc-2

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

<% 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作为IList(Of ConfigData.tblTT_Configuration)),然后从控制器中的值中提取值。谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

假设您的ConfigData.tblTT_Configuration模型具有名为NameEnvCdValue的属性,您需要做的就是修复TextBox名称以尊重{{3绑定到列表时:

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

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