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

后端 未结 2 581
生来不讨喜
生来不讨喜 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:31

    Rather than using TextBoxFor, just use the TextBox method, and you can specify the name you want it to use. Something like this should work, I think:

     @foreach (int i = 0; i < Model.Count; i++)
        {
            var gm = Model[i];
            
                    @Html.TextBox("gm.name" + i, gm.name, new {id = "gm_name" + i})
            
        }
    

提交回复
热议问题