Editing a Variable Length List, ASP.NET MVC 3 Style with Table

£可爱£侵袭症+ 提交于 2019-12-18 05:23:34

问题


I am following Steven Sanderson's blog post here to create an editable and variable length list of items. In his post he uses divs to display a new item in the list but I am using a table. Thus, my partial view for each item is rendering a tr tag with the various fields to edit. Right now my partial view looks something like this:

<tr>                
       @using (Html.BeginCollectionItem("LineItems"))
       {             
            <td>        
                @Html.TextBoxFor(m => m.Description)
                @Html.ValidationMessageFor(m => m.Description)                              
            </td>
            <td>
                @Html.TextBoxFor(m => m.Quantity)        
                @Html.ValidationMessageFor(m => m.Quantity)
            </td>
            <td>
                @Html.TextBoxFor(m => m.Amount)          
                @Html.ValidationMessageFor(m => m.Amount)       
            </td>
       }       
</tr>

This actually renders correctly on all browsers I have tested but the problem is that this really generates invalid HTML as it places a hidden input tag right after the opening tr tag.

<tr>
   <input type="hidden" name="LineItems.index" .... />
   <td>
      ...
   </td>
   ...
</tr>

There is a comment by another user on the linked post that says you can move the using statement into the first tag and it works but I haven't been able to get this to work using ASP.NET MVC 3 and the Razor view engine.

Does anyone have any idea how to use the logic presented by Steven Sanderson but get the hidden index input field inside the first td so as not to generate invalid HTML?

Thanks


回答1:


After a bit of experimenting I came up with:

<tr>                
   <td>        
   @using (Html.BeginCollectionItem("LineItems"))
   {             
            @Html.TextBoxFor(m => m.Description)
            @Html.ValidationMessageFor(m => m.Description)                              
        @:</td>
        @:<td>
            @Html.TextBoxFor(m => m.Quantity)        
            @Html.ValidationMessageFor(m => m.Quantity)
        @:</td>
        @:<td>
            @Html.TextBoxFor(m => m.Amount)          
            @Html.ValidationMessageFor(m => m.Amount)       
   }       
   </td>
</tr>

that did the trick for me.



来源:https://stackoverflow.com/questions/7153405/editing-a-variable-length-list-asp-net-mvc-3-style-with-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!