Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
965 views
in Technique[技术] by (71.8m points)

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...