I have a database table called FieldValue to store all the drop down list item and its option. It has 3 fields, fieldname, fieldvalu, fieldvaludesc, the fieldvaludesc is the option shown in the drop down list.
I have two models called ContactRecord and FieldValue, one of the fields MsgType will be shown in form of drop down list and its option is referring to the FieldValue table where FieldName = "MsgType"
FieldValue.cs
ContactRecord.cs
In the ContactRecordController, I have created the following code
In the Create.cshtml, I have no idea what to input in the asp-items. would anyone give me some advices, please?
Thank you for your help.
I have two models called ContactRecord and FieldValue, one of the fields MsgType will be shown in form of drop down list and its option is referring to the FieldValue table where FieldName = "MsgType"
FieldValue.cs
C#:
public class FieldValue
{
public string FieldName { get; set; }
public string FieldVal { get; set; }
public string FieldValueDesc { get; set; }
public char DefaultValue { get; set; }
}
ContactRecord.cs
C#:
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; }
[Required(ErrorMessage = "Please enter the Subject.")]
public string Subject { get; set; }
[Required(ErrorMessage = "Please enter your email.")]
[DataType(DataType.EmailAddress, ErrorMessage = "E-mail is not valid")]
public string Email { get; set; }
public string Status { get; set; }
}
In the ContactRecordController, I have created the following code
C#:
public ContactRecordsController(theManagerContext context)
{
_context = context;
}
// GET: ContactRecords
public async Task<IActionResult> Index()
{
return View(await _context.ContactRecord.ToListAsync());
}
private void PopulateMsgTypes(object selectedMsgType=null)
{
var MsgTypeQuery = from p in _context.FieldValue
orderby p.FieldValueDesc
select p;
ViewBag.FieldName = new SelectList(MsgTypeQuery, "FieldName", "FieldValueDesc", selectedMsgType);
}
// GET: ContactRecords/Create
public IActionResult Create()
{
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([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));
}
PopulateMsgTypes("MsgType");
return View(contactRecord);
}
In the Create.cshtml, I have no idea what to input in the asp-items. would anyone give me some advices, please?
C#:
<div class="form-group">
<label asp-for="MsgType" class="control-label"></label>
<select asp-for="MsgType" asp-items="??" class="form-control" />
<span asp-validation-for="MsgType" class="text-danger"></span>
</div>
Thank you for your help.