Rating in asp.net mvc4 entity framework

2019-08-30 18:06发布

问题:

I am trying rating in asp.net mvc4 with entity framework, I have tried this code in cshtml but it is not working, please help me to achieve this. Its working while i am running in normal browser but it is not working while integrating into the asp.net mvc4

{
  <h2>rating</h2>
  <form method="post" id="signin" action="@Url.Action("rating", "Rating")">
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

      <script type="text/javascript" src="D:\My Programs\FSLIndiaLatest\FSLIndiaLatest\Scripts\jquery.js"></script>
      <script type="text/javascript" src="D:\My Programs\FSLIndiaLatest\FSLIndiaLatest\Scripts\rating.js"></script>
      <link rel="stylesheet" type="text/css" href="D:\My Programs\FSLIndiaLatest\FSLIndiaLatest\Styles\rating.css" />
      <script type="text/javascript">
         $(function () {
            $('.rating').rating();
            $('.ratingEvent').rating({ rateEnd: function (v) { $('#result').text(v); } });
         });
      </script>
      <input type="text" class="ratingEvent rating9" value="5" />
      <div><b id="result">5</b> start(s)</div>
      <p>&nbsp;</p>
  </form>
}

code for rating.js {

(function ($)
{
    $.fn.rating = function (options)
    {
        var settings = $.extend(
            {
                rateEnd: function (value) { }
            }, options);

        function setRating(e, ul)
        {
            var i = parseInt(e.val());
            if (!i) { i = 0; }

            ul.find('a').removeAttr('class');
            ul.find('a:lt(' + i + ')').attr('class', 'full');
        }

        this.each(function ()
        {
            var e = $(this);
            var c = parseInt(e.attr("class").match(/rating\d+/)[0].replace('rating', ''));

            var ul = $('<ul class="rating"></ul>').insertAfter(e).width(c * 20 + 'px');

            if (c > 0)
            {
                for (k = 0; k < c; k++)
                {
                    ul.append('<li><a href="javascript:void(0);" title="' + (k + 1) + '">' + (k + 1) + '</a></li>')
                }
            }

            if (e.prop('readonly'))
            {
                var i = parseInt(e.val());

                if (!i) { i = 0; }

                ul.find('a').attr('title', i + ' / ' + c);
            }
            else
            {
                ul.find('a').each(function (index, link)
                {
                    var link = $(link);

                    link.hover(function ()
                    {
                        ul.find('a').removeAttr('class');
                        ul.find('a:lt(' + (index + 1) + ')').attr('class', 'hover');

                    }, function ()
                    {
                        setRating(e, ul);
                    });

                    link.click(function ()
                    {
                        e.val(index + 1);

                        setRating(e, ul);

                        settings.rateEnd(index + 1);
                    });
                });
            }

            setRating(e, ul);

            e.hide();
        });

        return this;
    }

})(jQuery);

}

code for rating.css

{

.rating { height: 20px; padding:0px; margin:0px; }
.rating li { list-style: none; float: left; width: 20px; height: 20px; padding:0px; margin:0px;}
.rating li a { display: block; width: 20px; height: 20px; padding:0px; margin:0px; overflow: hidden; text-indent: -100px; background-image: url(D:\My Programs\FSLIndiaLatest\FSLIndiaLatest\Images\rating.png); background-repeat: no-repeat; text-decoration:none; }
.rating li a:hover { background-color: transparent; text-decoration: none; }
.rating li .hover { background-position: 0px -20px; }
.rating li .full { background-position: 0px -40px; }

}

回答1:

I assume you're working with razor engine. If so, don't do this:

<script type="text/javascript" src="D:\My Programs\FSLIndiaLatest\FSLIndiaLatest\Scripts\jquery.js"></script>

Instead, do this:

<script type="text/javascript" src="~/Scripts/jquery.js"></script>

EDIT

This applies to all your .js and .css files referenced by their absolute local paths. If ~/ feature doesn't work for you, try using relative paths: src="/Scripts/jquery.js"

All the files referenced in your html should be included in your project.

It may have worked for you before because you were accessing a simple local html page. If the html page is hosted on a server (as it is, when you're running an MVC 4 application), it cannot access local files.



回答2:

I thing your JS file not load properly.put your JS file in your application JS folder and give relative path of JS then your code definitely work.