Add an index into mvchtmlstring in mvc

27 views Asked by At

I would like to add an index into mvchtmlstring because I use it in a table. If I need to get the whole list of the data input by user, I need an index so that the system are able to get all the list of data. So this is the example code that I want for:

@Html.CommonTextBoxFor(m => m.sample.RefNo, new { @class = "noMarginBottom", Name+="[0]" })

The point is I want something like this Name+="[0]"

I wish to get back the same name but adding a index after the name. So the name in the html will be like "name=m.sample.RefNo[0]".

Thanks for the help.

1

There are 1 answers

1
Tieson T. On

Sounds like you're after the NameFor helper. You'd use it like so:

@Html.CommonTextBoxFor(m => m.sample.RefNo, 
    new { 
        @class = "noMarginBottom", 
        name = Html.NameFor(m => m.sample.RefNo) + "[0]" 
    })

Seems like you have a mismatch between your view model and your controller code, though, if you need to do this - if RefNo should be indexed, the property should be a collection of some sort, so manually building the name attribute wouldn't be necessary.