2

When i am rating the star to 5 the id value should be passed as 10. It is passing the value as 1. If i rate the star to 4, the same value(1) is passing to the Controller. This is repeating for all the rated values,
I have delete all my previous code and added my updated code. Please check it out This is my View Code:

<form method="post" id="ratethecommand" action="@Url.Action("rated", "Rating")">
<table width="100%" cellspacing="10"> <tr>
  <td valign="top" width="">
   <table width="100%">
    <tr>
     <td width="50%">

     <a href="@Url.Action("rated/1", "Rating")"><input class="star {split:2}" type="radio" name="test-4-rating-3" /></a>
      <a href="@Url.Action("rated/2", "Rating")"><input class="star {split:2}" type="radio" name="test-4-rating-3" /></a>
       <a href="@Url.Action("rated/3", "Rating")"><input class="star {split:2}" type="radio" name="test-4-rating-3"/></a>
        <a href="@Url.Action("rated/4", "Rating")"><input class="star {split:2}" type="radio" name="test-4-rating-3" /></a>
         <a href="@Url.Action("rated/5", "Rating")"><input class="star {split:2}" type="radio" name="test-4-rating-3" /></a>
          <a href="@Url.Action("rated/6", "Rating")"><input class="star {split:2}" type="radio" name="test-4-rating-3" /></a>
           <a href="@Url.Action("rated/7", "Rating")"><input class="star {split:2}" type="radio" name="test-4-rating-3" /></a>
            <a href="@Url.Action("rated/8", "Rating")"><input class="star {split:2}" type="radio" name="test-4-rating-3" /></a>
             <a href="@Url.Action("rated/9", "Rating")"><input class="star {split:2}" type="radio" name="test-4-rating-3" /></a>
              <a href="@Url.Action("rated/10", "Rating")"><input class="star {split:2}" type="radio" name="test-4-rating-3" value="5.0"/></a>

<input type="text" name="rating"/>
    </td>

  <td valign="top" width="5">&nbsp;</td>  <td valign="top" width="160">
   <u>Test results</u>:<br/><br/>
   <div class="test Smaller">

    <span style="color:#FF0000">Results will be displayed here</span>
   </div>
  </td>
 </tr>
</table>

This is my Controller Code:

public ActionResult rated(int id)
        {
            double ratedvalue = Convert.ToDouble(id*0.5);
            Tbl_Rating tb = new Tbl_Rating();
            var value = new Tbl_Rating
            {
                Rating = ratedvalue,
                TaskId = 1,
                UserId = 1,
                DReportID = 1,
                CreatedBy = 1,
                CreatedOn = DateTime.Now,
                ModifiedBy = 1,
                ModifiedOn = DateTime.Now
            };

            db.Tbl_Rating.Add(value);
            db.SaveChanges();

            return View();
        }
Vetri
  • 347
  • 2
  • 6
  • 23

1 Answers1

2

This was the first solution, when the question was different.

It's trivial using jQuery: when user clicks on your radio/star you submit the form.

$(".star").click(function() {
   //your logic on star click
   $("#ratethecommand").submit();
});

You can also change your actual filosofy, doing a simple redirect instead of sumbitting the form: in this case you should transform your actual radio inputs (the stars) to A elements pointing to your controller/action and having the value as parameter (better if integer):

<a href="@Url.Action("rated/3", "Rating")">icon of your star</a>

If you have default routing then you can retrieve rate from id in your action (by the way: capitalize it!).

One last idea: you can also choose the asynchronous way, again with jQuery post and get methods it will be easy to implement!

Final solution (answer to last question edit)

Remove INPUT elements from A elements content. Insted put text / icons of stars.

Hope this helps,

Alberto

Alberto
  • 1,853
  • 1
  • 18
  • 22
  • I got UI for rating. But while clicking staring, It is showing error like resource can not be found. I gave correct path, and also showing error. Please help me out. – Vetri Feb 21 '14 at 10:29
  • Pass integer values, like 1,2,3. You can always divide by 2 in the action. – Alberto Feb 21 '14 at 10:51
  • Ok then how to get a rated value in Controller. I tried something but i cant get it. I have added my Controller code. Please check it out and help me – Vetri Feb 21 '14 at 10:58
  • public ActionResult rated(int id) – Alberto Feb 21 '14 at 11:23
  • No, It is not working. I didn't use any id in my action link, This is my CSHTML code : Whether have to add any parameter here?..If it's please let me know where to use in this code. – Vetri Feb 21 '14 at 11:37
  • Wait... you are mixing approaches and now you have input as A content. This is wrong. I told you how to fix your actual code OR to change filosophy. If you want to use your actual behavior (form POST) then use my JS to submit the forum. If you want to change and go with A elements (GET) then do something like this: icon of your star. Note that the value is in @Url.Action("rated/3", "Rating"), in this example 3. – Alberto Feb 21 '14 at 12:00
  • That @Url.Action("rated/3", "Rating") is translated to Rating/rating/3, which is routed by default routing rule to Controller: RatingController, Action: rating, id: 3. So if you have this action: public ActionResult rated(int id) in RatingController it works as inteded. Again: id is an integer so don't pass 0.5 or 4.5 for example! – Alberto Feb 21 '14 at 12:03
  • When i am use like this @Url.Action("rated/3", "Rating") , the value is stored into the database as 3 instead of my rated value(ie 1). – Vetri Feb 21 '14 at 12:22
  • That was an example! You need a different href for each A, according to the number of stars. – Alberto Feb 21 '14 at 12:26
  • So you'll have /1 for link with 1 star, /2 for link with 2 stars and so on – Alberto Feb 21 '14 at 12:28
  • Yes i got it, Thank you very much, Now its working. How to do it for decimal numbers like 0.5,1.5 like that?. Is there any other way to do it? – Vetri Feb 21 '14 at 12:48
  • Yes just pass 1 for half star, 2 for 1 star, 3 for 1.5 star and so on. Then in action do id/2. ;) try and tell me if you succeed – Alberto Feb 21 '14 at 12:52
  • No, Its is passing same value for all the rating. This is my star rating code for value 1. . If i rate 4, then also its taking first raging route value(1). I cant solve this problem. Please help me out – Vetri Feb 24 '14 at 09:26
  • I am using the same code. But its not working. Again i have added my modified View. Please Check it out. – Vetri Feb 24 '14 at 09:39
  • I don't see updated code... I still see things like this and this public ActionResult rated(int value)! Is really this your actual code? – Alberto Feb 24 '14 at 09:42
  • please check it now, I have added the View code. Yes Controller code is my actual code. – Vetri Feb 24 '14 at 09:45
  • Controller code is wrong. I told you to use id parameter and divide it by 2: public ActionResult rated(int id) { double ratedvalue = 0.5*id; – Alberto Feb 24 '14 at 09:50
  • I have changed it. but my problem is, for example when i rate a star as 4, it is passing the id value as 1. If i rate 5 then also it is passing the same id value 1. If i comment all the lines except id value 10 in View for star image, Then its passing the correct value as 10 to controller. This is my exact problem. Please help me to solve it – Vetri Feb 24 '14 at 10:12
  • I'll help you don't worry. Can you update your question with the actual view AND controller code? – Alberto Feb 24 '14 at 10:17
  • Moreover can you replace your A content (INPUT element) with a temporary text like these: half star, 3 stars, 1 star and half etc. – Alberto Feb 24 '14 at 10:20
  • I have deleted all the previous code,and posted with a updated code. Please check it out – Vetri Feb 24 '14 at 10:29
  • Great, now i'll check it. Meanwhile can you replace your A content (INPUT element) with a temporary text like these: half star, 3 stars, 1 star and half etc. Because I think the problem can be there ;) – Alberto Feb 24 '14 at 10:32
  • You are right. Problem with INPUT Element. I have changed it to text. Now its working properly. – Vetri Feb 24 '14 at 10:44
  • Glad to read it!!! Can I ask you to +1 my answer and comments that guided you to final working solution (A format, action parameter and A content I'd say). This way they will be highlighted to people with similar problem. – Alberto Feb 24 '14 at 10:47
  • Sure. Thank you very much for your help and your patience. When i am trying to make +1, Its is asking 15 reputation for it. Surely i will give +1 once getting 15 reputation. Once again thank you – Vetri Feb 24 '14 at 10:56
  • I'm happy to help new MVCers :-D – Alberto Feb 24 '14 at 11:05