Modelbinding IEnumerable in ASP.NET MVC POST?

前端 未结 2 1945
后悔当初
后悔当初 2021-02-20 12:46

Is there any issues with modelbinding IEnumerable types to an MVC POST?

Some properties in my Model are not being bound upon a post to an action. Seems that properties o

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-20 13:08

    Model binding depends upon how generated html looks like. for ur particular scenario to bind properly html should look like

    
    
    
    .
    .
    
    

    i have not tried it but i am almost certain that call to ToArray method in loop is keeping the system from generating proper names for nested inputs. There are couple of things you can do to remedy this First, in your view model change

    public IEnumerable BandAvailabilities { get; set; }
    

    to

    public IList BandAvailabilities { get; set; }  //or Array
    

    so you don't have to call ToArray method in the loop and proper names are generated for inputs. Second, make an editor template and put it in Editor templates folder either under the current controller or in shared folder's Editor template folder. Make this view accept model of type BandAvailabilityInfo and name of this view should also be BandAvailabilityInfo. then in your main view you only have to replace entire loop with

     <%: Html.EditorFor(m => m.ResponseInfo.SubsetInfo.BandAvailabilities%>
    

    and rest will be handled by framework itself

提交回复
热议问题