Jay Jordan
Jay Jordan

Reputation: 743

html.textbox naming asp.net mvc

I'm trying to iterate through a database and create a textbox for each record. Everything works just fine until I try to use the information in the controller. How do I pass this information back into the controller?

<% 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 %>

The controller looks like this:

    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

I might have been unclear about my question, but the answer I was looking for had to do with using the name of each cfg record (ie. cfg.Name, cfg.Value, cfg.EnvCd). I was trying to pass the parameters of cfg individually (ByVal name As String, ByVal value As String,), when I should have been passing cfg as a list or queryable(ByVal cfg As IList(Of ConfigData.tblTT_Configuration)), and then pulling the values from that in the controller. Thanks for the help guys.

Upvotes: 1

Views: 687

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039498

Assuming that your ConfigData.tblTT_Configuration model has properties called Name, EnvCd and Value all you need to do is fix your TextBox name in order to respect the naming convention when binding to a list:

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

Notice that you were missing a dot before the value.

Upvotes: 1

Related Questions