locked
Problem to default the value of drop down list populating the options from ViewModel RRS feed

  • Question

  • User-1140746197 posted

    Hi, 

    I have a webgrid to show the records of Message Type. User can edit it and pop up a window to edit the message type record (Model = OptMsgType). In this record, it has a status which is another model OptStatus. In the pop up window, I would like to default the status value according to the OptMsgType. I have put the line of code "Selected=x.StatusValue.ToString().Equals(recordStatus.ToString())? true:false" but it's not working. I would like to ask if there is any wrong with my code. (I chcecked that the recordStatus could be extracted correctly)

    Thank you.

    [HttpGet]
            public IActionResult Create(int id)
            {
                var OptMsgTypeViewModel = new OptMsgTypeCreateViewModel();
                OptMsgTypeViewModel.OptStatuses = new List<SelectListItem>();
                var temp = _context.OptStatus.ToList();
               
                var recordStatus = _context.OptMsgType.Where(m => m.MsgTypeId == id).Select(m => m.MsgStatus).SingleOrDefault();
                
                if (temp != null && temp.Count() > 0)
                {
                    if (id!=0) { 
                        OptMsgTypeViewModel.OptStatuses = temp.Select(x => new SelectListItem
                        {
                            Text = x.StatusName,
                            Value = x.StatusValue.ToString(),
                            Selected=x.StatusValue.ToString().Equals(recordStatus.ToString())? true:false
                        }).ToList();
                    }
                    else{
                        OptMsgTypeViewModel.OptStatuses = temp.Select(x => new SelectListItem
                        {
                            Text = x.StatusName,
                            Value = x.StatusValue.ToString()
                        }).ToList();
                    }
                }
    
                OptMsgTypeViewModel.OptMsgType = _context.OptMsgType.Where(a => a.MsgTypeId == id).FirstOrDefault();
                var v = _context.OptMsgType.Where(a => a.MsgTypeId == id).FirstOrDefault();
                return View(OptMsgTypeViewModel);
    
            }
    
            

    Saturday, May 9, 2020 3:00 AM

All replies

  • User-474980206 posted

    If you are using DropdownFor, you don't set the selected item in the option list, set set the model value, to selected item value you want.

    Saturday, May 9, 2020 3:16 PM
  • User1686398519 posted

    Hi,  thtang

    First of all, you should not set which value is selected in the controller, you can set it on the page.

    In addition, I suggest that you modify your code like this in the controller.

    Controller

    public IActionResult Create(int id)
    
            {
    
                var OptMsgTypeViewModel = new OptMsgTypeCreateViewModel();
    
                OptMsgTypeViewModel.OptStatuses = new List<SelectListItem>();
    
                var temp = _context.OptStatus.ToList();
    
                if (temp != null && temp.Count() > 0)
    
                {
    
                    OptMsgTypeViewModel.OptStatuses = temp.Select(x => new SelectListItem
    
                    {
    
                        Text = x.StatusName,
    
                        Value = x.StatusValue.ToString()
    
                    }).ToList();
    
                }
    
                OptMsgTypeViewModel.OptMsgType = _context.OptMsgType.Where(a => a.MsgTypeId == id).FirstOrDefault();
    
                var v = _context.OptMsgType.Where(a => a.MsgTypeId == id).FirstOrDefault();
    
                return View(OptMsgTypeViewModel);
    
            }

    Page

    @*new SelectList :The fourth parameter is the value selected by default*@
    
    @Html.DropDownListFor(n =>Model.OptMsgType.MsgStatus, new SelectList(Model.OptStatuses, "Value", "Text",Model.OptMsgType.MsgStatus))
    
    @Html.DropDownList("MsgStatus", new SelectList(Model.OptStatuses, "Value", "Text", Model.OptMsgType.MsgStatus))

    Here is the result.

    Best Regards,

    YihuiSun

    Monday, May 11, 2020 6:37 AM