I have controller method to render PartialView and show SelectList there
Here is method code
public ActionResult AddProject()
{
ViewBag.Region = new SelectList(db.Regions, "Id", "Name").ToList();
return PartialView("/Partials/ProjectModalPartial");
}
Here is code in PartialView
<div class="form-group">
<label for="recipient-name" class="col-form-label">Выберите Город</label>
@Html.DropDownList("Region", null, "XXXX", htmlAttributes: new { @class = "form-control", @id = "cityId"})
</div>
And here is how I call it in View
<div class="modal-body">
@Html.Partial("~/Views/Manage/Partials/ProjectModalPartial.cshtml")
</div>
Here is regions model
public partial class Region
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Region()
{
this.Cities = new HashSet<City>();
}
public int Id { get; set; }
public string Name { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<City> Cities { get; set; }
}
But I have this error
There is no ViewData item of type 'IEnumerable' that has the key 'Region'. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: There is no ViewData item of type 'IEnumerable' that has the key 'Region'.
Source Error:
Line 6: Line 7: Выберите Город Line 8: @Html.DropDownList("Region", null, "XXXX", htmlAttributes: new { @class = "form-control", @id = "cityId"}) Line 9: Line 10:
How can I solve it. And why it appears if I have this SelectList in code?