简体   繁体   中英

How To Make Count Of Dynamically Generated Controls In ASP.NET MVC

I have a form in a ASP.NET Mvc application, where one clicking an add button, a new set of controls - a dropdownlistfor and textbox is generated from the original. On clicking on submit, data values from some of the controls are passed via WebAPI to the DB while other values are used to generate a PDF using the iTextSharp library.

How can I count the number of dropdownlists and textboxes generated, for a specific feature, eg the 6 controls you see in the image 在此处输入图片说明 ?

How can I count the number of dropdownlists and textboxes generated,

Normally, you could use array, and default model binder could bind it for you. Please make sure controls ids are unique. For example, id="Unit_0" name="Unit[0]"

public class SampleViewModel
{
    public string[] Stock { get; set; }
    public string[] Unit { get; set; }
}

on click/submit (in jQuery)

    $('select').each(function(){

    //count your dropdown here

    });

    $('input[type=textbox]').each(function(){

    //count your textbox here

    })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM