mer_nam hai bulla rakhta hu kula
❔ 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; }
}
}
8 replies