使用MVC5进行星级评分

时间:2014-07-29 14:29:34

标签: rating-system

如何使用Mvc5实施星级评分?

我在此链接中找到了一个代码http://www.dotnetfunda.com/articles/show/2828/developing-a-star-rating-in-aspnet-mvc,我做了数据库,并添加了一个Controller和两个局部视图。 这是给我问题的局部视图。

@model string


@{
    Single m_Average = 0;

    Single m_totalNumberOfVotes = 0;
    Single m_totalVoteCount = 0;
    Single m_currentVotesCount = 0;
    Single m_inPercent = 0;
    var thisVote = string.Empty;

    if (Model.Length > 0)
    {
        // calculate total votes now
        string[] votes = Model.Split(',');

        for (int i = 0; i < votes.Length; i++)
        {
            m_currentVotesCount = Int16.Parse(votes[i]);
            m_totalNumberOfVotes = m_totalNumberOfVotes + m_currentVotesCount;
            m_totalVoteCount = m_totalVoteCount + (m_currentVotesCount * (i + 1));
        }

        m_Average = m_totalVoteCount / m_totalNumberOfVotes;
        m_inPercent = (m_Average * 100) / 5;

        thisVote = "<span style=\"display: block; width: 65px; height: 13px; background: url(/images/starRating.png) 0 0;\">" +
            "<span style=\"display: block; width: " + m_inPercent + "%; height: 13px; background: url(/images/starRating.png) 0 -13px;\"></span> " +
            "</span>" +
            "<span class=\"smallText\">Overall ratings: <span itemprop=\"ratingCount\">" + m_totalNumberOfVotes + "</span> | Rating: <span itemprop=\"ratingValue\">" + m_Average.ToString("##.##") + "</span> out of 5 </span>  ";
    }
}
<div itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">
    <meta itemprop="bestRating" content="5" />
    <meta itemprop="worstRating" content="1">
    <meta itemprop="ratingValue" content="@m_Average.ToString("##.##") %>" />
    @Html.Raw(thisVote)
</div>

当我运行我的应用程序时,我收到此错误:输入字符串不是正确的格式...

1 个答案:

答案 0 :(得分:0)

这个名字说明了所有“输入字符串不是正确格式”。你有输入字符串的地方?在Parse()方法上。

检查vote []数组的内容。我很确定其中一个值无法用数字解析。