在同一视图中多次执行相同的局部视图

时间:2014-03-19 12:32:18

标签: asp.net-mvc asp.net-mvc-3

我是MVC的新手,想要一些帮助。

我有一个视图(下方),它显示彼此相邻的产品。到这里一切都很好。

@foreach (var item in Model)
{
    <a href="@Url.Action("showProductDetails", "Shared", new { ProductID = item.ProductID }, null)">
        <div class='OutsideDiv'  >
            <table class='DivBorder'>  
                <tr >  
                    <td class='title'>  
                        @item.ProductName    
                    </td> 
                </tr> 
                <tr >  
                    <td class='imageBox'> 
                        <img alt='' src="@item.ImageURL" /> 
                    </td>  
                </tr> 
                <tr>  
                    <td class='title'> 
                       @Html.Action("showAverageRating", "Rating" , new { ProductID = item.ProductID }  ) *************

                    </td>
                </tr>  
                <tr>  
                    <td class='desc'>
                        @item.Description  
                    </td>
                </tr>   
                <tr>
                    <td class='price'>
                        € @item.Price 
                    </td>  
                </tr> 
            </table>
        </div> 
        <script type='text/javascript'>   $('.DivBorder').mouseover(function () { $(this).css('border-color', '#0953cb'); $(this).css('background-color', '#eaeaea'); }); $('.DivBorder').mouseout(function () { $(this).css('border-color', '#bdbdbd'); $(this).css('background-color', '#f6f6f6'); });</script> 
    </a>
}

在标有&#39; ****&#39;的行中上面我正在调用另一个视图(showAverageRating),现在只显示5个评级星,前3个开始被选中。

<input class="star  " name="adv1" type="radio" /> 
<input class="star  " name="adv1" type="radio" />
<input checked="checked" class="star   " name="adv1" type="radio" /> 
<input class="star  " name="adv1" type="radio" /> 
<input class="star  " name="adv1" type="radio" />

问题在于,在第二项上,当再次调用评级星形视图(在局部视图上方)时,星星显示在第一个产品的星星旁边,如下图所示。

enter image description here

我想我必须使用别的东西而不是Html.Action

编辑:showAverageRating代码

public ActionResult showAverageRating(int ProductID)
{
    decimal averageRating = new RatingsService.RatingsClient().getAverageRating(ProductID);
    ViewData["averageRating"] = averageRating;
    return PartialView();
}

1 个答案:

答案 0 :(得分:1)

问题是星星的名称相同,所以我将productID与其名称一起包括在内,如下所示。

showAverageRating局部视图

@{decimal averageRating = ViewBag.averageRating;}
@{int productID = ViewBag.productID;}

<div id="averageRating"  >
    <input class="star" name="star+@productID" type="radio" /> 
    <input class="star  " name="star+@productID" type="radio" />
    <input checked="checked" class="star   " name="star+@productID" type="radio" /> 
    <input class="star  " name="star+@productID" type="radio" /> 
    <input class="star  " name="star+@productID" type="radio" />  
</div>