Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to update GroupTest table passing by idGroupTest.

What I have tried:

C#
public ActionResult GroupTestUpdateById(int idGroupTest)
     {
         GroupTestUIModel groupTestUIModel = new GroupTestUIModel();

         MedicalTestBL medicalTestBL = new MedicalTestBL();

         var config = new MapperConfiguration(cfg =>
         {
             cfg.CreateMap<GroupTestModelBL, GroupTestUIModel>();
         });
         IMapper iMapper = config.CreateMapper();
         groupTestUIModel = iMapper.Map<GroupTestModelBL, GroupTestUIModel>(medicalTestBL.GroupTestUpdateById(idGroupTest));

         List<SelectListItem> selectTestListDDL = medicalTestBL.TestCategoryFormList().Select(x => new SelectListItem
         {
             Value = x.idTestCategory.ToString(),
             Text = x.TestCategoryName
         }).ToList();
         selectTestListDDL.Insert(0, new SelectListItem { Text = "Select", Value = "" });

         groupTestUIModel = new GroupTestUIModel()
         {
             selectTestCategoryType = selectTestListDDL
         };

         return View("GroupTestForm", groupTestUIModel);

     }


But I can't append both the dropdownlist items and GroupTest model object to `groupTestUIModel`. How can I do that?
Posted
Updated 19-Jul-21 3:27am

1 solution

Rather than creating a new instance of your view-model, set the property on the existing instance:
C#
// Don't do this:
// groupTestUIModel = new GroupTestUIModel()
// {
//    selectTestCategoryType = selectTestListDDL
// };

// Instead, do this:
groupTestUIModel.selectTestCategoryType = selectTestListDDL;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900