I want to add a DropDownList in my Insert View.
Controller:
public ActionResult Create()
{
ViewBag.CategoryList = new SelectList(Category.GetCategories());
return View("Insert", new Category());
}
View:
@Html.DropDownListFor(model => model.Category, (IEnumerable<SelectListItem>)
(ViewBag.CategoryList), "Select Category", htmlAttributes: new { @class = "form-control" })
The Category class has only one string field named Title. Instead of getting the value of Title I'm getting WebApplication.Models.Category.
return View("Insert", new Category())you bind new instance of theCategoryclass to themodelproperty of your razor page. It means the type of themodelproperty isCategoryitself. So it looks a bit strange statement likemodel.Categorybecause the model shouldn't have this property but should havemodel.Title. Couldn't you please clarify how you declare @model on the page.Productmodel which has aCategoryproperty.@modelon the page is@model WebApplication1.Models.Productreturn View("Insert", new Product())in the controller method. After that you be able to address to the Title property viamodel.Category.Titlestatement or bind your dropdownbox to the Category property usingm => m.Category.