开发者

Get posted values from dynamically added controls in ASP.NET MVC

开发者 https://www.devze.com 2023-02-12 18:56 出处:网络
I have a form in which i\'m dynamically addding controls through jQuery. I need to access the values in those controls (textboxes) when posting back the form to the server. I\'m sur开发者_StackOverflo

I have a form in which i'm dynamically addding controls through jQuery. I need to access the values in those controls (textboxes) when posting back the form to the server. I'm sur开发者_StackOverflowe this is a trivial problem but i just can't get my head around it.

Any help would be greatly apreciated.


When adding a multiple controls to the page, give them all the same name attribute so you can do the following in your action:

public ActionResult MyAction(string[] items)
{
     // items will contain all the values in the text boxes
    return View();
}

So your HTML would like like this

<input type="text" name="items" />
<input type="text" name="items" />
<input type="text" name="items" />
<input type="text" name="items" />
<input type="text" name="items" />
<input type="text" name="items" />
0

精彩评论

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