2014-02-16 48 views
0

我有對象的列表<>,併爲每個對象,我創建一個表中的一行:根據Counter修改控件名稱?

@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> 

...

正如你所看到的,但是,在代碼中,我在標記中硬編碼了「0」。我需要這個來反映「for」循環中的Cntr變量的值。

@onchange = "populateSubCategory(0)"

我怎麼能代替0,以@cntr?

回答

2

您可以簡單地使用字符串連接:

new { @onchange = "populateSubCategory(" + cnt + ")", @class = "cmbCategory0 form-control" }) 

或者使用string.Format()

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

我不是一個MVC傢伙,但爲什麼你不能直接連接字符串,

新{ 「平變化= \」 populateSubCategory( 「cnt.ToString(), 」\「,類= \ 」cmbCategory「,cnt.ToString(), 」形狀控制\「」})