1

I'm making a test page for a project I'm working on and I've made desired progress so far but I'm trying to create TextBoxes from a model of List being passed to the view, however, the it seems to just ignore anything I have tried.

<form id="form1" runat="server">
    <input id="btnsubmit" type="submit" name="Submit" onclick="Submit" />
    <div id="divControls">
     <% foreach (TextBox control in (this.Model as List<TextBox>))
        {
            Html.Label("lblLabel", control.Text);
            Html.TextBox(control.ID, control.Text, new { id = control.ID, style = "width:50", name = "txt" + control.ID });
        } %>
    </div>
</form>

The List isn't null in the Controller on return. I don't have a clue at what the problem could be. If I throw a

Something

in the for loop it executes the appropriate number of times so why isn't it creating the labels or textboxes? At first I thought it was that I'm adding them inside a form but I removed the form tags and it still didn't work so I really have no Idea, any help would be much appreciated. I'm relatively new to MVC.

[HttpPost]
        public ActionResult Index(FormCollection form)
        {
            List<TextBox> controls = new List<TextBox>();
            foreach (String Key in form.Keys)
            {
                if (Key.Contains("txt"))
                {
                    TextBox textBox = new TextBox();
                    textBox.ID = Key;
                    textBox.Text = form.GetValues(Key)[0];
                    controls.Add(textBox);
                }
            }
            return View("Index", controls);
        }

Here's my Action encase it's helps. Also encase I wasn't clear enough, I am adding controls to a form at runtime using JQuery and then that Action will be part of the submit so it must send the textboxes back to the view so they are not deleted. Like I said I'm new to the whole MVC and Asynchronous thing so If there's a better way to do this, advice would be much appreciated.

2 Answers 2

1

Your not printing the html

<% foreach (TextBox control in (this.Model as List<TextBox>))
        {%>
            <%=Html.Label("lblLabel", control.Text)%>
            <%=Html.TextBox(control.ID, control.Text, new { id = control.ID, style = "width:50", name = "txt" + control.ID })%>
<%        } %>

Your code is looping through the controls and the Html.whaterever is returning a string but your not doing anything with it, just discarding it.

you also don't need to return a whole TextBox object. This is probably inefficient. Just return an struct or a class containing your data

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

Comments

0

Html.Label returns a string containing a <label> tag.
You're discarding that string.

You need to write it to the page by writing <%= Html.Whatever() %>.

2 Comments

It says "Expected )" for the line <%= Html.Label("lblLabel", control.Text); %>
Thank you. I had tried that before but didn't realize you don't use semicolons so i got another exception.

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.