The ViewData item that has the key 'SelectedRating' is of type 'System.Int32' but must be of type 'IEnumerable'.
This is the error I get when I'm trying to add a DropDownList via partial view. I tried to follow this solution but I somehow failed to do so due to my low experience in coding.
This is how my code look for the moment:
[HttpGet]
public ActionResult Rating()
{
RateReviewVM model = new RateReviewVM()
{
ReviewId = id,
RatingList = new SelectList(Enumerable.Range(1, 10))
};
return View(model);
}
[HttpPost]
public ActionResult Rating(RateReviewVM model) {
if (!ModelState.IsValid)
{
model.RatingList = new SelectList(Enumerable.Range(1, 10));
return View(model);
}
//...
View for _RatingPartial
@model xxxx.ViewModel.RateReviewVM
<h6>Rate the review</h6>
using (Html.BeginForm()) {
<div class="form-group">
@Html.LabelFor(m => m.SelectedRating)
@Html.DropDownListFor(m => m.SelectedRating, Model.RatingList, "-Please select-", new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.SelectedRating)
</div>
<button type="submit" class="btn btn-default submit">Rate</button>
}
}
This is how I try to display _RatingPartial view
@{
var newRating = new xxxx.ViewModel.RateReviewVM { ReviewId = Model.Id };
Html.RenderPartial("_RatingPartial", newRating);
}
Model
public class RateReviewVM {
public System.Guid Id { get; set; }
public System.Guid UserId { get; set; }
public System.Guid ReviewId { get; set; }
public bool HasLiked { get; set; }
public Nullable<int> Rating { get; set; }
public int SelectedRating { get; set; }
public IEnumerable<SelectListItem> RatingList { get; set; }
}
EDIT: ---
Details View(Main)
@model xxxx.Review
@{
ViewBag.Title = "Details";
}
@*@{
//var newRating = new xxxx.ViewModel.RateReviewVM { ReviewId = Model.Id };
@Html.RenderPartial("_RatingPartial", newRating)
}*@
<h2>Details</h2>
<div>
<h4>Review</h4>
<hr/>
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.Title)
</dt>
<dd>
@Html.DisplayFor(model => model.Title)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Description)
</dt>
<dd>
@Html.DisplayFor(model => model.Description)
</dd>
@* and so on.. *@
</dl>
</div>
<p>
@Html.ActionLink("Back to List", "Index")
</p>
Controller for Details
public ActionResult Details(Guid? id) {
Review review = db.Reviews.Find(id);
return View(review);
}
Model Review for Details
public System.Guid Id { get; set; }
public System.Guid CreatorUserId { get; set; }
[Required]
public string Title { get; set; }
[Required]
public string Description { get; set; }
public System.DateTime CreatedDate { get; set; }
//etc - some irrelevant property
public virtual ICollection<RateReview> Users { get; set; }