Fighting SelectList in ASP.NET MVC 2

I have a model that looks something like this:

public class SampleModel
{
    public static SampleModel Create()
    {
        return new SampleModel
        {
            Boolean = true,
            // set several more properties...
            Colors = new SelectList(new[] { "Red", "Green", "Blue" }, "Green")
        };
    }

    public bool Boolean { get; set; }
    // define several more properties...
    public SelectList Colors { get; set; }
}

I let ASP.NET MVC automatically align properties using Html.DisplayForModel()for my presentation of information and Html.EditorForModel()for my kind of editing.

Results:

  • Editing works great. Colorsappears as a menu with three items (red, green, and blue), and green is selected by default.

  • However, to represent the information, I get "False True False", which is apparently a list of values IsSelectedfor each menu item. I don’t want that. I would like it to just display Green.

  • , UpdateModel(sampleModel) , : " ". , -, , Colors SelectList, SelectList , .

, , , , , , , , .

:

  • Colors "", (, "" ) ""?
  • " " , SelectList?
  • /ViewModel , ColorId Colors, , Details, , , , Color . , LinqToSql (SqlMetal) .
+3
1

, SelectList . , , , - :

  • 3 : "" , LinqToSql ( ) , "" "".

  • . "" , FK , .

  • Edit . , .

  • "", , " " . , , -.

, View, Edit, Details. , SelectList , .

+3

Source: https://habr.com/ru/post/1732815/


All Articles