user1085906
user1085906

Reputation: 13

MVC 3 RAZOR create text box dynamically

How to create textbox dynamically based on item count in collection using mvc 3 razor. Also once created how do we read the values entered in those.

Upvotes: 0

Views: 2082

Answers (1)

Ed B
Ed B

Reputation: 6054

What i've done is create a helper in a .cshtml file in my App_Code folder that creates a textbox:

@helper CreateTextBox(string id)
{
    <input type="text" id="@id"/>
}

Then in my view I can create the textboxes dynamically:

@foreach (var item in Model.Applications)
{
    @MyHelpers.CreateTextBox("textBoxAppId" + item.ApplicationId)       //you can also use your own index here
}

When trying to read my input after a postback, I just loop thru parameters in the Request.Form that start with 'textBoxAppId'

Upvotes: 2

Related Questions