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

5
  • 1
    You model needs a property to bind to - say public int SelectedCategory { get; set; } (and it should also have a IEnumerable<SelectListItem> property for the options. Refer this Q/A for typical code to generate and bind a <select> Commented Jun 27, 2018 at 8: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 Commented Jun 27, 2018 at 8: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. Commented Jun 27, 2018 at 8: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<CategoryModelDum> 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. Commented Jun 27, 2018 at 8: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. Commented Jun 27, 2018 at 8:33

1 Answer 1

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;
Sign up to request clarification or add additional context in comments.

6 Comments

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?
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 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?
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.
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
|

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.