简体   繁体   English

如何在ASP.NET MVC中统计动态生成的控件

[英]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. 我在ASP.NET Mvc应用程序中有一个表单,在其中单击一个添加按钮,一组新控件-从原始文档生成一个dropdownlistfor和textbox。 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. 单击提交时,来自某些控件的数据值将通过WebAPI传递到数据库,而其他值则用于通过iTextSharp库生成PDF。

How can I count the number of dropdownlists and textboxes generated, for a specific feature, eg the 6 controls you see in the image 如何计算特定功能(例如,您在图像中看到的6个控件)生成的下拉列表和文本框的数量 在此处输入图片说明 ?

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. 请确保控件ID是唯一的。 For example, id="Unit_0" name="Unit[0]" 例如, id="Unit_0" name="Unit[0]"

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

on click/submit (in jQuery) 点击/提交时(在jQuery中)

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

    //count your dropdown here

    });

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

    //count your textbox here

    })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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