locked
Problem in binding dropdown list from database RRS feed

  • Question

  • User-1140746197 posted

    I have an issued to bind the drop down list from table DDLMsgType in database , when I build the project, I get the following error. I would like to ask if there are anything wrong in the data type used in my work. Thanks!

    Severity Code Description Project File Line Suppression State  Error CS0266 Cannot implicitly convert type 'System.Collections.Generic.List<theManager.Models.DDLMsgType>' to 'System.Collections.Generic.IEnumerable<Microsoft.AspNetCore.Mvc.Rendering.SelectListItem>'. An explicit conversion exists (are you missing a cast?) theManager Views\ContactRecords\Create.cshtml

        public partial class ContactRecord
        {
            public long ContactId { get; set; }
    
            [Required(ErrorMessage = "Please enter your name."), MaxLength(50)]
            [Display(Name = "Name")]
            public string SenderName { get; set; }
            
            [Required(ErrorMessage = "Please select the Message Type")]
            [Display(Name = "Message Type")]
            public string MsgType { get; set; }
            public List<DDLMsgType> MsgTypeSL { get; set; }
      public class DDLMsgType
            {
                [Required]
                public int MsgTypeID { get; set; }
                [Required]
                public string MsgType { get; set; }
                [Required]
                public string MsgTypeStatus { get; set; }     
    
            }

    In the Controller

    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
            [HttpPost]
            [ValidateAntiForgeryToken]
            public async Task<IActionResult> Create([Bind("ContactId,SenderName,MsgType,Subject,Email,ContactNo,CreateTime,FollowedUpBy,Status")] ContactRecord contactRecord)
            {
                if (ModelState.IsValid)
                {
                    _context.Add(contactRecord);
                    await _context.SaveChangesAsync();
                    return RedirectToAction(nameof(Index));
                }
                
                ContactRecord c = new ContactRecord();
                c.MsgTypeSL = _context.DDLMsgType.ToList<DDLMsgType>();
                return View(contactRecord);
            }
    

    In the Create.cshtml

     <form asp-action="Create">
                <div asp-validation-summary="ModelOnly" class="text-danger"></div>
                <div class="form-group">
                    <label asp-for="ContactId" class="control-label"></label>
                    <input asp-for="ContactId" class="form-control" />
                    <span asp-validation-for="ContactId" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="SenderName" class="control-label"></label>
                    <input asp-for="SenderName" class="form-control" />
                    <span asp-validation-for="SenderName" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="MsgType" class="control-label"></label>
                    <select asp-for="MsgType" asp-items="Model.MsgTypeSL" class="form-control" />
                    <span asp-validation-for="MsgType" class="text-danger"></span>
                </div>

    Wednesday, November 13, 2019 12:04 PM

All replies

  • User475983607 posted

    The select tag helper expects a collection of SelectLisItems but you pass the tag helper a List<DDLMsgType>.  See the following tutorial which illustrates how to populate a select.

    https://www.learnrazorpages.com/razor-pages/forms/select-lists

    Wednesday, November 13, 2019 12:26 PM
  • User-719153870 posted

    Hi thtang,

    Cannot implicitly convert type 'System.Collections.Generic.List<theManager.Models.DDLMsgType>' to 'System.Collections.Generic.IEnumerable<Microsoft.AspNetCore.Mvc.Rendering.SelectListItem>

    Seems you are trying to pass a DDLMsgType list to where it only accept SelectListItem.

    Below is a simple demo you can refer to:

    Model:

    public class ContactRecord
        {
            public long ContactId { get; set; }
    
            [Required(ErrorMessage = "Please enter your name."), MaxLength(50)]
            [Display(Name = "Name")]
            public string SenderName { get; set; }
    
            [Required(ErrorMessage = "Please select the Message Type")]
            [Display(Name = "Message Type")]
            public string MsgType { get; set; }
            public DDLMsgType MsgTypeSL { get; set; }       
    }
    

    View:

    <div class="row">
        <div class="col-md-4">
            <form asp-action="Create">
                <div asp-validation-summary="ModelOnly" class="text-danger"></div>
                <div class="form-group">
                    <label asp-for="SenderName" class="control-label"></label>
                    <input asp-for="SenderName" class="form-control" />
                    <span asp-validation-for="SenderName" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="MsgType" class="control-label"></label>
                    <select asp-for="MsgType" asp-items="ViewBag.MsgType" class="form-control" ></select>
                    <span asp-validation-for="MsgType" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <input type="submit" value="Create" class="btn btn-primary" />
                </div>
            </form>
        </div>
    </div>
    

    Controller:

    public IActionResult Create()
            {
                ViewBag.MsgType = new SelectList(_context.DDLMsgType.ToList(), "MsgTypeID", "MsgType");
                return View();
            }
    
            // POST: ContactRecords/Create
            // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
            // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
            [HttpPost]
            [ValidateAntiForgeryToken]
            public async Task<IActionResult> Create(ContactRecord contactRecord)
            {
                if (ModelState.IsValid)
                {
                    _context.Add(contactRecord);
                    await _context.SaveChangesAsync();
                    return RedirectToAction(nameof(Index));
                }
                return View(contactRecord);
            }
    

    Best Regard,

    Yang Shen

    Thursday, November 14, 2019 8:45 AM