ASP MVC集合编辑

时间:2015-02-11 09:07:07

标签: javascript asp.net-mvc asp.net-mvc-4 collections

我试过了项目 ivanz.com/2011/06/16/editing-variable-length-reorderable-collections-in-asp-net-mvc-part-1 /

这是一个很好的例子,但我不知道如何获得总评分 因为id就像这样

'FavouriteMovies_98812e66-80f2-453b-a0a8-bf85d8bc6ae0__Rating'

通常如果id是

'FavouriteMovies_0__Rating'
'FavouriteMovies_1__Rating'
'FavouriteMovies_2__Rating' 

我使用javascript

像这样

for(var i = 0; i <= rowCount-1;i++){
 summary += $('#FavouriteMovies_' + i + '__Rating').val();
}

请有人帮助我

这是我html的完整标签

<fieldset>
        <legend>My Favourite Movies</legend>

        <ul style="list-style-type: none" id="movieEditor" class="ui-sortable">
<li style="padding-bottom:15px">
<input type="hidden" value="b231eea2-79fc-499d-a286-7d30dbc3be9c" name="FavouriteMovies.Index" autocomplete="off">
        <img alt="" style="cursor: move" src="/Content/images/draggable-icon.png">
<label for="FavouriteMovies_b231eea2-79fc-499d-a286-7d30dbc3be9c__Title">Title</label><input type="text" value="Movie 1" name="FavouriteMovies[b231eea2-79fc-499d-a286-7d30dbc3be9c].Title" id="FavouriteMovies_b231eea2-79fc-499d-a286-7d30dbc3be9c__Title" data-val-required="The Title field is required." data-val="true" class="text-box single-line"><span data-valmsg-replace="true" data-valmsg-for="FavouriteMovies[b231eea2-79fc-499d-a286-7d30dbc3be9c].Title" class="field-validation-valid"></span><label for="FavouriteMovies_b231eea2-79fc-499d-a286-7d30dbc3be9c__Rating">Rating</label><input type="text" value="5" name="FavouriteMovies[b231eea2-79fc-499d-a286-7d30dbc3be9c].Rating" id="FavouriteMovies_b231eea2-79fc-499d-a286-7d30dbc3be9c__Rating" data-val-required="The Rating field is required." data-val-number="The field Rating must be a number." data-val="true" class="rating"><span data-valmsg-replace="true" data-valmsg-for="FavouriteMovies[b231eea2-79fc-499d-a286-7d30dbc3be9c].Rating" class="field-validation-valid"></span>        <a onclick="$(this).parent().remove();" href="#">Delete</a>
</li>
<li style="padding-bottom:15px">
<input type="hidden" value="5d85f979-0b96-4ba5-9f7a-a1ffe25d406a" name="FavouriteMovies.Index" autocomplete="off">
        <img alt="" style="cursor: move" src="/Content/images/draggable-icon.png">
<label for="FavouriteMovies_5d85f979-0b96-4ba5-9f7a-a1ffe25d406a__Title">Title</label><input type="text" value="Movie 2" name="FavouriteMovies[5d85f979-0b96-4ba5-9f7a-a1ffe25d406a].Title" id="FavouriteMovies_5d85f979-0b96-4ba5-9f7a-a1ffe25d406a__Title" data-val-required="The Title field is required." data-val="true" class="text-box single-line"><span data-valmsg-replace="true" data-valmsg-for="FavouriteMovies[5d85f979-0b96-4ba5-9f7a-a1ffe25d406a].Title" class="field-validation-valid"></span><label for="FavouriteMovies_5d85f979-0b96-4ba5-9f7a-a1ffe25d406a__Rating">Rating</label><input type="text" value="10" name="FavouriteMovies[5d85f979-0b96-4ba5-9f7a-a1ffe25d406a].Rating" id="FavouriteMovies_5d85f979-0b96-4ba5-9f7a-a1ffe25d406a__Rating" data-val-required="The Rating field is required." data-val-number="The field Rating must be a number." data-val="true" class="rating"><span data-valmsg-replace="true" data-valmsg-for="FavouriteMovies[5d85f979-0b96-4ba5-9f7a-a1ffe25d406a].Rating" class="field-validation-valid"></span>        <a onclick="$(this).parent().remove();" href="#">Delete</a>
</li>
<li style="padding-bottom:15px">
<input type="hidden" value="57fb472a-ae66-427a-8a46-0af771d8997e" name="FavouriteMovies.Index" autocomplete="off">
        <img alt="" style="cursor: move" src="/Content/images/draggable-icon.png">
<label for="FavouriteMovies_57fb472a-ae66-427a-8a46-0af771d8997e__Title">Title</label><input type="text" value="Movie 3" name="FavouriteMovies[57fb472a-ae66-427a-8a46-0af771d8997e].Title" id="FavouriteMovies_57fb472a-ae66-427a-8a46-0af771d8997e__Title" data-val-required="The Title field is required." data-val="true" class="text-box single-line"><span data-valmsg-replace="true" data-valmsg-for="FavouriteMovies[57fb472a-ae66-427a-8a46-0af771d8997e].Title" class="field-validation-valid"></span><label for="FavouriteMovies_57fb472a-ae66-427a-8a46-0af771d8997e__Rating">Rating</label><input type="text" value="12" name="FavouriteMovies[57fb472a-ae66-427a-8a46-0af771d8997e].Rating" id="FavouriteMovies_57fb472a-ae66-427a-8a46-0af771d8997e__Rating" data-val-required="The Rating field is required." data-val-number="The field Rating must be a number." data-val="true" class="rating"><span data-valmsg-replace="true" data-valmsg-for="FavouriteMovies[57fb472a-ae66-427a-8a46-0af771d8997e].Rating" class="field-validation-valid"></span>        <a onclick="$(this).parent().remove();" href="#">Delete</a>
</li>
        </ul>
        <input type="text" value="" name="SummaryRating" id="SummaryRating">

        <a href="#" id="addAnother">Add another</a>
        <script type="text/javascript">
            $(function () {
                $("#movieEditor").sortable();

                function reAmount() {
                    $.get('/User/MovieRatingSummaryColumn', function (data) {
                        $("#SummaryRating").val(data.sum);
                    });
                }

                $(".rating").on('change', function () {
                    alert(this.id);
                    reAmount();
                });

                $("#addAnother").click(function () {
                    $.get('/User/MovieEntryRow', function (template) {
                        $("#movieEditor").append(template);
                    });

                });
            });
        </script>
    </fieldset>

1 个答案:

答案 0 :(得分:0)

要获取Rating属性的所有输入的总和,您可以使用

var total = 0; 
$('.rating').each(function() { 
  total += new Number($(this).val());
});