How to concatenate two data in a ViewData at the razor page in ASP.Net Core?

asp.net-core c# entity-framework-core

Question

I created a ViewData[] to populate a drop down list. There are 3 fields (Id, Grade and ClzName) in ViewData[]. I need to set ID field to Id and Value field to Grade+ClzName of the drop down list.

I try it as follows but failed. Any one can please help me to resolve it.

controller.cs

 private void PopulateSelectClassDDL()
        {
            using (_context)
            {
                List<Class> classList = new List<Class>();
                classList = _context.Classes.ToList();               
                ViewData["classList"] = classList;

            }
        }

view.cshtml

  @Html.DropDownList("ClzId", new SelectList((System.Collections.IEnumerable)ViewData["classList"],"Id", "Grade" + "ClzName"))
1
0
4/4/2020 4:57:00 AM

Accepted Answer

Add a property to the model that returns what you want and use that –by Sami Kuhmonen

Then I add following line into my model. [NotMapped] public string className=> $"{Grade} {ClzName}"

0
4/9/2020 12:42:26 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow