1

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.

3
  • At the statement return View("Insert", new Category()) you bind new instance of the Category class to the model property of your razor page. It means the type of the model property is Category itself. So it looks a bit strange statement like model.Category because the model shouldn't have this property but should have model.Title. Couldn't you please clarify how you declare @model on the page. Commented Feb 8, 2022 at 14:52
  • @Kitta I have a Product model which has a Category property. @model on the page is @model WebApplication1.Models.Product Commented Feb 8, 2022 at 15:05
  • That means that you have discrepancy between instance that you passed to the page and your model declaration. I guess, first of all you need something like return View("Insert", new Product()) in the controller method. After that you be able to address to the Title property via model.Category.Title statement or bind your dropdownbox to the Category property using m => m.Category. Commented Feb 8, 2022 at 15:21

1 Answer 1

2

Check that the Title is defined as property and not a field to be data binding work properly.

Then try the following:

public ActionResult Create()
{
    ViewBag.CategoryList = Category.GetCategories();
    return View("Insert", new Product()); 
}

And in the Insert view:

@using WebApplication1.Models @* Category namespace *@
@model WebApplication1.Models.Product

@{   
    IEnumerable<Category> categories = (IEnumerable<Category>)ViewBag.CategoryList;
}

@Html.DropDownListFor(m => categories.GetEnumerator().Current,
          categories.Select(d =>
          {
              return new SelectListItem() { Text = d.Title, Value = d.Title };
          }),
          "Select Category", new { @class = "form-control" })
Sign up to request clarification or add additional context in comments.

6 Comments

@EL02: Can you explain why you pass one instance of the Category to the Insert view as the view model? It looks strange. It would be more logical to pass list of the all categories, instead of using the ViewBag.
I too am kinda lost here. First time dealing with dropdownlists. I have a Product model which has a string Category property. @model on the page is @model WebApplication1.Models.Product. I want to pass the value of the selected option to model.Category
@EL02: Ok. Then in the Create method just replace Category.GetCategories(); with code that populate the ViewBag.CategoryList collection. It should be of type List<Category>.
I get this error: Unable to cast object of type 'System.Web.Mvc.SelectList' to type 'System.Collections.Generic.IEnumerable1[WebApplication1.Models.Category]'. And yes i did also change the controller
@EL02: It's because you have ViewBag.CategoryList = new SelectList(Category.GetCategories()); line in the Create method. I suggest replace this line by ViewBag.CategoryList = Category.GetCategories(); one. I suppose the Category.GetCategories() creating an List<Category>.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.