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);
}
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.
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; }
}
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);
}
Member
32 Points
224 Posts
Problem in binding dropdown list from database
Nov 13, 2019 12:04 PM|thtang|LINK
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
In the Controller
In the Create.cshtml
All-Star
53641 Points
23992 Posts
Re: Problem in binding dropdown list from database
Nov 13, 2019 12:26 PM|mgebhard|LINK
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
Contributor
3140 Points
983 Posts
Re: Problem in binding dropdown list from database
Nov 14, 2019 08:45 AM|Yang Shen|LINK
Hi thtang,
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:
View:
Controller:
Best Regard,
Yang Shen