开发者

Asp.net MVC dynamic generated Text Boxes

I am creating a page with with some Text Boxes that are generated dynamically. Ids' of all text boxes are also generated at run time. I want to sen开发者_开发问答d all text boxes value to my controller and save that data. How I get all text boxes value. I may use javascript or JQuery. Can anyone suggest me?

Thanks in advance..

Ashish


In asp.net mvc you are allowed to generate html code directly in the view. For example:

<% for(int i = 0; i < 10; i++) { %>
  <input type='text' name='text-<%=i%>' id='text-<%=i%>' value="My box <%=i%>" />
<% } %>

This can be generated by JQuery as well. If you have the data at server side, it's best to use this way. If they need to be computed client side, then use JQuery.

You can also use the HTML Helper:

<%=Html.TextBox("name", "default value")%>

You can add parameters to that too, if you need to set the ID for example.

Then you will retrieve this in your controller, by adding a FormCollection to the arguments and reading from that.

[HttpPost]
public ActionResult Example(int id, FormCollection post) {
  // Here FormCollection["text-0"] is equal to "My box 0"
}


Or maybe you just iterate all values from

Request.Form.Item

in your controller-action... Just use a simple for-each-loop to go through each submitted value.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜