Reputation: 31
How To Populate Data in the dropdownlist from a Database in c# MVC5 using Code First.
This is What I have Tried:
Model:
public class EventBooking
{
[Key]
public int EventId { get; set; }
[Required(ErrorMessage = "Enter your full name ")]
public string FullName { get; set; }
[Required(ErrorMessage = "Enter your phone number ")]
[DataType(DataType.PhoneNumber)]
public string PhoneNumber { get; set; }
[Required(ErrorMessage = "Enter your email address ")]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
[Required(ErrorMessage = "Specify your event start date ")]
[DataType(DataType.Date)]
public DateTime StartDate { get; set; }
[Required(ErrorMessage = "Specify your event end date ")]
[DataType(DataType.Date)]
public DateTime EndDate { get; set; }
//[Required(ErrorMessage = "Select your event")]
public string EventType { get; set; }
public string EventDescription { get; set; }
[Required(ErrorMessage = "Specify the number of your event attendees ")]
public int NumberOfAttendes { get; set; }
}
Controller:
public ActionResult Create([Bind(Include="EventId,FullName,PhoneNumber,Email,StartDate,EndDate,EventType,EventDescription,NumberOfAttendes")] EventBooking eventBooking)
{
if (ModelState.IsValid)
{
db.EventBooking.Add(eventBooking);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(eventBooking);
}
View:
<div class="form-group">
@Html.LabelFor(model => model.EventType, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.EventType, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.EventType, "", new { @class = "text-danger" })
</div>
</div>
I am unable to change the view Of Events type into a Drop Down. can anyone please help.
Upvotes: 1
Views: 1412
Reputation: 655
To bind DropDownList in MVC5 as follow :
View Code
@model DepenInjectionINMVC5App.Controllers.DemoController.EventBooking
@{
ViewBag.Title = "CreateDDL";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>CreateDDL</h2>
<div>
@Html.LabelFor(Model => Model.Id, "Event", new { @class = "control-label" })
@Html.DropDownListFor(Model => Model.Id, new SelectList(ViewBag.EventList, "Id", "Name"))
</div>
Controller Code
public class EventBooking
{
public int Id { get; set; }
public string Name { get; set; }
}
public ActionResult CreateDDL()
{
var eventList = new List<EventBooking>();
EventBooking eventItem;
var eventArr = new string[] { "Event-1", "Event-2", "Event-3", "Event-4", "Event-5", "Event-6", "Event-7" };
for (int index = 0; index < eventArr.Length; index++)
{
eventItem = new EventBooking();
eventItem.Id = index + 1;
eventItem.Name = eventArr[index];
eventList.Add(eventItem);
}
ViewBag.EventList = eventList;
return View();
}
Upvotes: 1
Reputation: 130
I've seen a couple of different ways of populating dropdowns with data, but the way I end up doing it usually revolves around getting your 'event types' into its own list, and sending that back to the view along with the model. The default scaffolded action uses the ViewBag, but you can create your own ViewModel or tack on a list with metadata..
I assume you mean the Get action rather than the Post?
Controller:
// GET: /Create
public ActionResult Create()
{
EventBookingViewModel eventBookingViewModel = new EventBookingViewModel
{
EventTypes = new SelectList(methodToReturnAListToHere(), "ValueColumn", "DisplayColumn")
};
return View(eventBookingViewModel);
}
View:
<div class="col-md-9">
@Html.DropDownListFor(model => model.EventType, Model.EventTypes, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.EventType)
</div>
In the DropDownListFor, the first parameter is the one you want to store the selected value in, and the second is the SelectList to which you wish to bind.
Upvotes: 1
Reputation: 1
you want to populate a list of event? Try Something like these maybe it might work for you
<tr>
<td>Event type</td>
<td>
<select @Html.EditorFor(model => model.EventType)>
<option>//enter you list of items you want to display here</option>
</select>
@Html.ValidationMessageFor(model => model.EventType, "", new { @class = "text-danger" })
</td>
Upvotes: 0