0

I have an application that is written using C# on the top of Asp.Net MVC 5.

I have the following model

public class SomeViewModel
{
    public IList<CheckBoxListItem> Days { get; set; }
    ......
}


public class CheckBoxListItem
{
   public bool Checked { get; set; }
   public string Value { get; set; }
   public string Text { get; set; }
} 

I want to be able to get the ModelMetadata for a single day aka CheckBoxListItem in the Days collection.

I tried the following to get the first day in the day's collection

// Attempt to get the ModelMetadata for the first item in the Days collection
ModelMetadata.FromStringExpression("Days[0]", html.ViewData); 

// Attempt to get the ModelMetadata  for the Checked property in the first item
ModelMetadata.FromStringExpression("Days[0].Checked", html.ViewData); 

But the above does not return the ModelMetadata.

The following code returns the whole collection as expected.

ModelMetadata.FromStringExpression("Days", html.ViewData); 

What is the correct expression to use to get the ModelMetadata for a single item in a collection?

Andrei
  • 55,890
  • 9
  • 87
  • 108
Junior
  • 11,602
  • 27
  • 106
  • 212
  • Possible duplicate of [MVC Html Helper: modelmetadata from string expression](https://stackoverflow.com/questions/12471684/mvc-html-helper-modelmetadata-from-string-expression) – Andrei Sep 26 '19 at 21:51
  • Please see the answer in the suggested dupe, it explains why what you are doing does not work – Andrei Sep 26 '19 at 21:52
  • @Andrei I still don't know the cause. `Days` is not null and has 7 items. The `ModelMetadata.FromStringExpression("Days", html.ViewData);` returns the expected data. The problem is that getting the item in `Days` returns nothing – Junior Sep 26 '19 at 23:01

0 Answers0