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;cnt@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" })