First of all, don't add the
SelectList
to your database entity. At best, it will be ignored; otherwise, it will generate errors because Entity Framework doesn't understand how to store or load it.
I would recommend using a view-model to serve as the model for your view. Add the
SelectList
to the view-model. Write a method to populate the
SelectList
, and call it before displaying your create or edit view.
Database entity:
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; }
}
View model:
public class ContactRecordViewModel
{
public ContactRecordViewModel(ContactRecord record)
{
ContactId = record.ContactId;
SenderName = record.SenderName;
MsgType = record.MsgType;
}
public ContactRecordViewModel()
{
}
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 SelectList MsgTypeSL { get; set; }
public void UpdateModel(ContactRecord record)
{
record.SenderName = SenderName;
record.MsgType = MsgType;
}
}
Controller:
private async Task PopulateLookups(ContactRecordViewModel model)
{
var msgTypes = _context.DDLMsgType.OrderBy(p => p.MsgType).ToListAsync();
model.MsgTypeSL = new SelectList(msgTypes, nameof(DDLMsgType.MsgType), nameof(DDLMsgType.MsgType), model.MsgType);
}
[HttpGet]
public async Task<IActionResult> Create()
{
var model = new ContactRecordViewModel();
await PopulateLookups(model);
return View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(ContactRecordViewModel model)
{
if (!ModelState.IsValid)
{
await PopulateLookups(model);
return View(model);
}
var record = new ContactRecord();
model.UpdateModel(record);
_context.ContactRecord.Add(record);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
[HttpGet]
public async Task<IActionResult> Edit(long id)
{
var record = await _context.ContactRecord.FindAsync(id);
if (record is null) return NotFound();
var model = new ContactRecordViewModel(record);
await PopulateLookups(model);
return View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(ContactRecordViewModel model)
{
var record = await _context.ContactRecord.FindAsync(model.ContactId);
if (record is null) return NotFound();
if (!ModelState.IsValid)
{
await PopulateLookups(model);
return View(model);
}
model.UpdateModel(record);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
NB: Based on your
DDLMsgType
entity, it looks like the
MsgType
property in your
ContactRecord
should probably be an
int
linking to the
MsgTypeID
. In which case, you'll need to change the view-model to match, and change the
dataValueField
parameter to the
SelectList
constructor.