Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是否基于计数器修改控件名称?_C#_Asp.net Mvc - Fatal编程技术网

C# 是否基于计数器修改控件名称?

C# 是否基于计数器修改控件名称?,c#,asp.net-mvc,C#,Asp.net Mvc,我有一个对象列表,对于每个对象,我在表中创建一行: @for(int cnt=0; cnt< Model.Transaction.TransactionLines.Count; cnt++) { <div class="form-group"> <label for="@("cmbCategory" + cnt.ToString())" class="col-lg-1 control-label">Category:</label>

我有一个对象列表,对于每个对象,我在表中创建一行:

@for(int cnt=0; cnt< Model.Transaction.TransactionLines.Count; cnt++)
{
    <div class="form-group">
    <label for="@("cmbCategory" + cnt.ToString())" class="col-lg-1 control-label">Category:</label>
    <div class="col-lg-3">
        @Html.DropDownListFor(x => x.Transaction.TransactionLines[cnt].CategoryId,
                                  new SelectList(Model.TransactionReferences.Categories, "Value", "Text", Model.Transaction.TransactionLines[cnt].CategoryId), "Select one",
                                  new { @onchange = "populateSubCategory(0)", @class = "cmbCategory0 form-control" })
    </div>
(int cnt=0;cntx.Transaction.TransactionLines[cnt].CategoryId, 新建SelectList(Model.TransactionReferences.Categories,“值”、“文本”、Model.Transaction.TransactionLines[cnt].CategoryId)、“选择一个”, 新建{@onchange=“populateSubCategory(0)”,@class=“cmbCategory0表单控件”})

正如您所看到的,在代码中,我已经在标记中硬编码了“0”。我需要它来反映“for”循环中Cntr变量的值

i、 e.
@onchange=“populateSubCategory(0)”


我如何用@cntr替换0?

我不是MVC,但为什么你不能连接字符串

e、 g


新的{“onchange=\”populateSubCategory(“,cnt.ToString(),“\”,class=\“cmbcontegory”,cnt.ToString(),“form control\”)

您可以简单地使用字符串连接:

new { @onchange = "populateSubCategory(" + cnt + ")", @class = "cmbCategory0 form-control" })
或者使用
string.Format()

new { @onchange = string.Format("populateSubCategory({0})", cnt), @class = "cmbCategory0 form-control" })