简体   繁体   English

如何在ASP.NET MVC中应用评级

[英]How to apply rating in ASP.NET MVC

i am trying to implement star rating in MVC but find it very difficult, because most rating on the internet uses jquery where the querystring of current rating value is inside jquery code.i want to be able to rate each movie on my site, but cannot be able to pass my movieID to MovieRating action. 我正在尝试在MVC中实施星级评分,但发现非常困难,因为互联网上的大多数评分都使用jquery,其中当前评分值的查询字符串位于jquery代码内。我希望能够对我网站上的每部电影进行评分,但不能能够将我的movieID传递给MovieRating操作。 this because the query string that is being pass has already been taken by rating. 这是因为正在传递的查询字符串已被评定采用。 i also try by using FormCollection to collect my movieID but was recieving null value. 我也尝试通过使用FormCollection来收集我的movieID,但接收到空值。 i also want to display each user rating when they logon on to the site. 我也想在他们登录该网站时显示每个用户的评分。 Please any help will be highly appreciate. 请任何帮助将不胜感激。

public ActionResult MovieRating(int id, int rating, MovieRating MovieRating)
{
    MovieRating.Rating= rating;
    MovieRating.ProfileID = "duru";

    MovieRating.MovieID =id;
    //save change not yet implemented    
}

this is the code in my view: this Url.RequestContext.RouteData.Values["id"] help me get the query string of the current movieID 这是我看来的代码:此Url.RequestContext.RouteData.Values [“ id”]帮助我获取当前movieID的查询字符串

@using (Html.BeginForm("MovieRating", "Movie", FormMethod.Post, new { id =          Url.RequestContext.RouteData.Values["id"] }))
{
    <p>
      <img src="../../Content/RatingImages/EmptyStar.png"  class="MovieRating"  alt="Star Rating" align="middle" id="1" />
      <img src="../../Content/RatingImages/EmptyStar.png" class="MovieRating" alt="Star Rating" align="middle" id="2" />
      <img src="../../Content/RatingImages/EmptyStar.png" class="MovieRating" alt="Star Rating" align="middle" id="3" />
      <img src="../../Content/RatingImages/EmptyStar.png" class="MovieRating" alt="Star Rating" align="middle" id="4" />
      <img src="../../Content/RatingImages/EmptyStar.png" class="MovieRating" alt="Star Rating" align="middle" id="5" />
   </p>
   <div id="result"></div>
}

this the jquery code 这是jQuery代码

/// <reference path="jquery-1.5.1-vsdoc.js" />
/// <reference path="jquery-ui-1.8.11.js" />
/*(document).(function(){*/
$(function () {
    $('.MovieRating').mouseover(function () {
        giveRating($(this), "FilledStar.png");
        $(this).css("cursor", "pointer");
    });

    $('.MovieRating').mouseout(function () {
        giveRating($(this), "EmptyStar.png");
    });

    $('.MovieRating').click(function () {
        $('.MovieRating').unbind("mouseout mouseover click");

        // call ajax methods to update database
        var url = "/Movie/MovieRating?rating=" + parseInt($(this).attr("id"));
        $.post(url, null, function (data) {
            $("#result").text(data);
        });
    });
});

function giveRating(img, image) {
    img.attr("src", "/Content/RatingImages/" + image)
        .prevAll('.MovieRating').attr("src", "/Content/RatingImages/" + image);
}

I would assume that when you're building the jQuery call that posts the rating to your action that you have the ability to specify the url that the rating should be posted to. 我假设在构建将评分发布到动作的jQuery调用时,您可以指定应将评分发布到的url。 If there is no way to extend the jquery plugin to allow additional parameters, I would post to the route 'Controller/MovieRating/@Model.MovieId' and then allow the jQuery plugin to post whatever query string arguments that it needs. 如果无法扩展jquery插件以允许其他参数,我将发布到路由'Controller/MovieRating/@Model.MovieId' ,然后让jQuery插件发布所需的任何查询字符串参数。 Once you receive this in your controller, id will be populated and you can continue assigning your rating to the movie 在控制器中收到此消息后,将填充id然后您可以继续为电影分配评分

NOTE: the route provided assumes you're using razor syntax and since you didn't post much code regarding your view or your controller, that your viewmodel has the Movie Id as MovieId, you will obviously have to change this to suit your needs 注意:提供的路由假定您使用的是razor语法,并且由于您没有发布太多有关视图或控制器的代码,因此您的视图模型将Movie Id作为MovieId,显然您必须更改此设置以适合您的需求

EDIT after seeing your code, if you change this line 如果您更改此行,请在看到代码后进行编辑

var url = "/Movie/MovieRating?rating=" + parseInt($(this).attr("id"));

to this: 对此:

var url = "/Movie/MovieRating/@Url.RequestContext.RouteData.Values["id"]?rating=" + parseInt($(this).attr("id"));

it should get you in the correct direction. 它应该为您提供正确的方向。 This will allow your MovieRating action to accept the id parameter that it's expecting. 这将使您的MovieRating操作可以接受期望的id参数。

However, another question that I have for you, are you passing a viewmodel to your page? 但是,我还有一个问题要问您,您是否将视图模型传递给您的页面? Why wouldn't you be passing the Id of the Movie through on your viewmodel rather than grabbing it from the RouteData each time? 为什么不每次在ViewModel上传递电影的ID而不是每次都从RouteData中获取它? Just curious 只是好奇

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM