❔ Models data is not showing in view with help of ViewData

I have created model and passed its data with ViewData like this- Controller Code--> IEnumerable<SelectListItem> CoverTypeList = _UnitOfWork.CoverType.GetAll().Select( u => new SelectListItem { Text = u.Name, Value = u.Id.ToString(), } ); if (id == null || id == 0) { ViewData["CoverTypeList"] = CoverTypeList; return View(product); } <--- to View--> <label asp-for="CoverTypeId"></label> <select asp-for="CoverTypeId" asp-items="@(ViewData["CoverTypeId"] as IEnumerable<SelectListItem>)" class="form-select"> <option disabled selected>--Select CoverType--</option> </select> <span asp-validation-for="CategoryId" class="text-danger"> </span> <---Model---> namespace BulkyBook.Models { public class CoverType { [Key] public int Id { get; set; } [Display(Name ="Conver Type")] [Required] [MaxLength(100)] public string Name { get; set; } } }
4 Replies
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
mer_nam hai bulla rakhta hu kula
thnaks Bro it worked you are savior
Angius
Angius3y ago
Just pass a strongly-typed model instead of the ViewData magic strings tomfoolery Eliminates 100% of those issues
Accord
Accord3y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server