locked
How to return the Text of Selectd value of SelectList? RRS feed

  • Question

  • User991566988 posted

    Hi

    I've added new properties to the ApplicationUser Class:

        public class ApplicationUser : IdentityUser
        {
            public ApplicationUser()
            {
                MarkterStatus = false;
            }
            public string FullName { get; set; }
            public bool MarkterStatus { get; set; }
    
        }

    Then child class of the previous:

        public class Orders
        {
            public int Id { get; set; }
    
            public string MarkterId { get; set; }
    
            public string MarkterName { get; set; }
    
            public ApplicationUser Markter { get; set; }
        }

    Then I used MarkterStatus in Where to populate SelectList  like this in the controller:

                List<SelectListItem> selectList = _context.ApplicationUser.Where(x => x.MarkterStatus == true).OrderBy(x => x.FullName).Select(x => new SelectListItem { Text = x.FullName ?? "", Value = x.Id }).ToList();
                ViewData["MarkterId"] = selectList;

    Now how to save the ApplicationUser.FullName text value returned by the previous SelectList in MarkterName property of Orders?

    Tuesday, March 3, 2020 3:07 PM

Answers

  • User-474980206 posted

    The browser only posts back the selected value. To include the text, will require a hidden field and client script path at copies the option text to the hidden field whenever the value is changed. A better approach is to lookup the text again in the post back controller.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 3, 2020 3:13 PM

All replies

  • User-474980206 posted

    The browser only posts back the selected value. To include the text, will require a hidden field and client script path at copies the option text to the hidden field whenever the value is changed. A better approach is to lookup the text again in the post back controller.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 3, 2020 3:13 PM
  • User991566988 posted

    you are right, I searched for it and it works now. Thanks

    Tuesday, March 3, 2020 3:41 PM