5 star rating in mail using c mvc
Here is an example of how to implement a 5-star rating system using C# MVC:
Model
Create a new model class Rating
to store the rating data:
public class Rating
{
public int Id { get; set; }
public int MovieId { get; set; }
public int RatingValue { get; set; } // 1-5
public string Comment { get; set; }
public DateTime DateAdded { get; set; }
}
Controller
Create a new controller class RatingsController
to handle the rating data:
public class RatingsController : Controller
{
private readonly DbContext _dbContext;
public RatingsController(DbContext dbContext)
{
_dbContext = dbContext;
}
// GET: Ratings
public ActionResult Index()
{
var ratings = _dbContext.Ratings.ToList();
return View(ratings);
}
// POST: Ratings
[HttpPost]
public ActionResult Create(Rating rating)
{
if (ModelState.IsValid)
{
_dbContext.Ratings.Add(rating);
_dbContext.SaveChanges();
return RedirectToAction("Index");
}
return View(rating);
}
// GET: Ratings/Edit/5
public ActionResult Edit(int id)
{
var rating = _dbContext.Ratings.Find(id);
return View(rating);
}
// POST: Ratings/Edit/5
[HttpPost]
public ActionResult Edit(Rating rating)
{
if (ModelState.IsValid)
{
_dbContext.Entry(rating).State = EntityState.Modified;
_dbContext.SaveChanges();
return RedirectToAction("Index");
}
return View(rating);
}
// GET: Ratings/Delete/5
public ActionResult Delete(int id)
{
var rating = _dbContext.Ratings.Find(id);
return View(rating);
}
// POST: Ratings/Delete/5
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
var rating = _dbContext.Ratings.Find(id);
_dbContext.Ratings.Remove(rating);
_dbContext.SaveChanges();
return RedirectToAction("Index");
}
}
View
Create a new view Index.cshtml
to display the ratings:
@model IEnumerable<Rating>
<h2>Ratings</h2>
<table>
<tr>
<th>Movie Title</th>
<th>Rating Value</th>
<th>Comment</th>
<th>Date Added</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>@item.Movie.Title</td>
<td>@item.RatingValue</td>
<td>@item.Comment</td>
<td>@item.DateAdded.ToString("yyyy-MM-dd")</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id = item.Id })
</td>
</tr>
}
</table>
<h2>Add Rating</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Rating</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.MovieId, "Movie", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("MovieId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.MovieId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.RatingValue, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.RatingValue, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.RatingValue, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Comment, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Comment, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Comment, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
JavaScript
Add the following JavaScript code to the view to handle the rating submission:
$(document).ready(function () {
$('#RatingValue').rating({
size: 'xs',
starCaption: 'Rating: {0}/5',
starEmpty: '☆',
starHalf: '★',
starFull: '★'
});
$('#RatingForm').submit(function (e) {
e.preventDefault();
var ratingValue = $('#RatingValue').val();
var comment = $('#Comment').val();
$.ajax({
type: 'POST',
url: '@Url.Action("Create", "Ratings")',
data: { ratingValue: ratingValue, comment: comment },
success: function (data) {
alert('Rating submitted successfully!');
window.location.href = '@Url.Action("Index", "Ratings")';
}
});
});
});
This code uses the jQuery Rating plugin to create a 5-star rating system. When the user submits the form, it sends a POST request to the Create
action method in the RatingsController
to create a new rating.
Note: This is just a basic example and you may want to add additional validation, error handling, and security measures to your application.