0

Before drafting this question I went through some links of this type, and Implemented the same type of code block but unable to find out what mistake am I doing.

Problem.

I have view model which contains 2 list data and one is of the list which I want to display it into dropdownlist control.

Viewmodel

public class ClaimVM
{
  public List<ClaimHistoryModel> claimHistoryModel { get; set; }
  public List<CategoryModelDum> categoryModel { get; set; }
}

CategoryModelDum.cs

public class CategoryModelDum
{
    public string CategoryName { get; set; }
    public Int16 ID { get; set; 
}

currently I am receiving data for both the lists and I am passing this viewmodel from controller to VIew(since one of the list is working as expected).

here is my dropdownlist code

@Html.DropDownListFor(m => m.categoryModel.Select(x => x.CategoryName), new SelectList(Model.categoryModel, "ID", "CategoryName"), "Select Category", new { @class = "ddlList" })

so I need categorymodel data to be displayed on dropdownlist. but this is throwing exception saying:-

Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions

I am pretty sure that I am doing some silly mistake, but any help will be appreciated.

UPDATE - 1

here is the code snipped which works as expected. but not with view model class.

@model.ClaimHistory.Models.CategoryModelDum

 @Html.DropDownListFor(model => model.CategoryName, new SelectList(ViewBag.CategoryType, "ID", "CategoryName", Model.ID), new { @class = "ddlList" })

here is the dropdownlist enter image description here

Manjuboyz
  • 6,978
  • 3
  • 21
  • 43
  • 1
    You model needs a property to bind to - say `public int SelectedCategory { get; set; }` (and it should also have a `IEnumerable` property for the options. Refer [this Q/A](https://stackoverflow.com/questions/34366305/the-viewdata-item-that-has-the-key-xxx-is-of-type-system-int32-but-must-be-o) for typical code to generate and bind a ` –  Jun 27 '18 at 08:08
  • `m => m.categoryModel.Select(x => x.CategoryName)` the first argument for `@Html.DropDownListFor` should be something like `m => m.SelectedCategory` and your model should have a property `public int SelectedCategory {get;set;}` as said by Stephen Muecke – Rafalon Jun 27 '18 at 08:11
  • No Offence are you sure that is how it works ? because I have same type of same model which works fine as expected, check Update-1. – Manjuboyz Jun 27 '18 at 08:20
  • Well just look at the first argument, you have `model => model.CategoryName` in your working code, and `m => m.categoryModel.Select(x => x.CategoryName)` in your non-working code. See the difference? To be clear, I didn't say you shouldn't have `public List categoryModel { get; set; }` in your model, and that you shouldn't use it for the `SelectList` part. I said you should have an additional property which will hold the selected value, as you did in your working code. – Rafalon Jun 27 '18 at 08:24
  • So I have to use another property to handle this list? or is there a way I can remove selectList from VIew and bind the data to dropdownlist from existing model? reason, at present I am getting proper data in controller as I need and I don't want to modify that design. – Manjuboyz Jun 27 '18 at 08:33

1 Answers1

1

The following (small) changes should work:

public class ClaimVM
{
    // will hold the selected category ID
    public Int16? CategoryID { get; set; }

    public List<ClaimHistoryModel> claimHistoryModel { get; set; }
    public List<CategoryModelDum> categoryModel { get; set; }
}

@Html.DropDownListFor(m => m.CategoryID, new SelectList(Model.categoryModel, "ID", "CategoryName"), "Select Category", new { @class = "ddlList" })
// change here -------^^^^^^^^^^^^^^^^^

To be clear, the first parameter of @Html.DropDownListFor is here to tell what property will be bound with the selected value.
Therefore it needs to be an assignable variable.
You can not write:

Model.categoryModel.Select(x => x.CategoryName) = Model.categoryModel[selectedIndex].ID;

But you can indeed write:

Model.CategoryID = Model.categoryModel[selectedIndex].ID;
Rafalon
  • 4,450
  • 2
  • 16
  • 30
  • I have a question here, CategoryID property is is not related to any class or list, how this is linked to the object I need? – Manjuboyz Jun 27 '18 at 09:15
  • I will have multiple dropdownlist in the same page hence multiple model in view model, so should I keep this categoryID common or can I use same ID for all dropdown models ? – Manjuboyz Jun 27 '18 at 09:20
  • @Manjuboyz It depends on what you want to do with your dropdownlists. If you don't want to bind the selected value to a variable, then maybe you should try to look for `@Html.DropDownList` instead of `@Html.DropDownListFor`? – Rafalon Jun 27 '18 at 09:42
  • True, I will be having multiple dropdownlist in the same view and one should pop up data based on another dropdown selection, so I have that confusion which one to use.. hope you can answer this question. is it good to use DropDownlist or DropDownListFor ? and yes, will go through blogs on those controls. – Manjuboyz Jun 29 '18 at 03:31
  • `DropDownListFor` will suit if you have to pass the selected value to the controller. If you only use the selected value say with javascript, then `DropDownList` should be enough – Rafalon Jun 29 '18 at 06:28
  • Ok, I have to pass the values further and current workflow is working with DropDownListFor will retain that, thanks for you answer :) – Manjuboyz Jun 29 '18 at 06:44