1

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.

1

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Yeah, I was missing the dot, and that was a mistake from me trying to figure out the problem. My issue is that I'm not passing back the name and value parameters to the controller. Do I need to change the name of the textbox or do I need to change how the controller recognizes the parameters?
I notice that your controller action is taking a name string parameter. If you want to pass a value to it you should use <%: Html.TextBox("Name", "Some value you want to pass to controller") %>. If on the other hand you want to populate the Name property of your ConfigData.tblTT_Configuration model (which your controller action is taking a list of) then your current code is correct: "cfg[" & i & "].Name". But this assumes that you have multiple names.
Yeah, it works for iterating through the records, but it doesn't pass the contents of the textbox back to the controller. Basically, my problem is in Function NewEnvCd(ByVal newEnvironment As String, ByVal name As String, ByVal value As String, ByVal cfg As IList(Of ConfigData.tblTT_Configuration)) name and value are empty. How do I pass the info from the textbox back to the controller?
If you want the name argument to be populated you should have a hidden field or textbox inside your form with the same name: <%: Html.TextBox("Name", "Some value you want to pass to controller") %>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.