how to prevent html input id duplication on asp.net mvc 3 when a model contains multiple elements

后端 未结 2 547
生来不讨喜
生来不讨喜 2021-01-27 11:03

I have a view that allows the user to inset multiple elements of the same type.

 @foreach (var gm in Model)
    {
        
                         


        
2条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-27 11:49

    For what it's worth you probably want.

    
            
    
    
            
    
    

    It appears from the source code that the ExpressionHelper method used to translate the name from the model expression understands about array indexing expressions. You might try something like:

    @for (int i = 0; i < Model.Count; ++i )
    {
       
                @Html.TextBoxFor(m => m[i].name)
        
    }
    

    I'll confess that I haven't tried this in MVC3 so I'm not sure whether it will work or not. In earlier versions I built this sort of thing by hand.

提交回复
热议问题