0

I am new to ASP.Net MVC4 with Entity Framework. I am trying sample rating. The problem is while i am passing the rated value from View to Controller, I am not able to get it. Please help me to get it. Thanks in advance.

This is my View Code:

<body>
<div class="Clear" id="wrap">
<div id="tab-Testing">

<script type="text/javascript">
    $(function () {
        $('#form1 :radio.star').rating();
        $('#form2 :radio.star').rating({ cancel: 'Cancel', cancelValue: '0' });
        $('#form3 :radio.star').rating();
        $('#form4 :radio.star').rating();
    });
</script>
<script type="text/javascript">
    $(function () {
        $('#tab-Testing form').submit(function () {
            $('.test', this).html('');
            $('input', this).each(function () {
                if (this.checked) $('.test', this.form).append('' + this.name + ': ' + this.value + '<br/>');
            });
            return false;
        });
    });
</script>


 <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" value="1"/></a>
      <a href="@Url.Action("rated/2", "Rating")"><input class="star {split:2}" type="radio" name="test-4-rating-3" value="2"/></a>
       <a href="@Url.Action("rated/3", "Rating")"><input class="star {split:2}" type="radio" name="test-4-rating-3" value="1.5"/></a>
        <a href="@Url.Action("rated/4", "Rating")"><input class="star {split:2}" type="radio" name="test-4-rating-3" value="2.0"/></a>
         <a href="@Url.Action("rated/5", "Rating")"><input class="star {split:2}" type="radio" name="test-4-rating-3" value="2.5"/></a>
          <a href="@Url.Action("rated/6", "Rating")"><input class="star {split:2}" type="radio" name="test-4-rating-3" value="3.0"/></a>
           <a href="@Url.Action("rated/7", "Rating")"><input class="star {split:2}" type="radio" name="test-4-rating-3" value="3.5"/></a>
            <a href="@Url.Action("rated/8", "Rating")"><input class="star {split:2}" type="radio" name="test-4-rating-3" value="4.0"/></a>
             <a href="@Url.Action("rated/9", "Rating")"><input class="star {split:2}" type="radio" name="test-4-rating-3" value="4.5"/></a>
              <a href="@Url.Action("rated/10", "Rating")"><input class="star {split:2}" type="radio" name="test-4-rating-3" value="5.0"/></a>

    <div><small><pre class="code"><code class="html"></code></pre></small></div>

   </div>
  <td valign="top" width="5">&nbsp;</td>  <td valign="top" width="50">


    <input type="text" name="rating"/>
    @*<input type="submit" value="Submit!" />*@
    </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>
</form>
   </div>
   </body>

This is my Controller code:

[HttpPost]
        public ActionResult rated(int id)
        {
            int ratedvalue = id;
            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

0

only for helpers (except display) tie view data to the model. Use the radio button for for your radio button

@Html.RadioButtonFor(x => x.rated, "1")
@Html.RadioButtonFor(x => x.rated, "2")

with this the value rated in your model will be set to whatever value was selected on the view

Matt Bodily
  • 6,403
  • 4
  • 29
  • 48
  • I just tried with ActionLink. How to add rating image for the radio button. I have added my code for ActionLink. Please help me to get rating image in radio button. – Vetri Feb 24 '14 at 07:52
  • try here http://viralpatel.net/blogs/css-radio-button-checkbox-background/ I believe to accomplish what you want you need to put a class on the radio button and set the background image through css – Matt Bodily Feb 24 '14 at 14:21