MVC5的下拉列表
本文关键字:下拉列表 MVC5 | 更新日期: 2023-09-27 18:13:44
如何在c# MVC5中使用Code First从数据库中填充下拉列表中的数据。
This is What I have try:
模型:
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; }
}
控制器:
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);
}
视图:
<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>
我无法将事件视图类型更改为下拉菜单。有谁能帮帮我吗?
我见过几种用数据填充下拉框的不同方式,但我最终做的方式通常是围绕将您的'事件类型'放入其自己的列表中,并将其与模型一起发送回视图。默认的脚手架操作使用ViewBag,但是你也可以创建你自己的ViewModel或者添加一个包含元数据的列表。
我想你是指Get动作而不是Post?
控制器:
// GET: /Create
public ActionResult Create()
{
EventBookingViewModel eventBookingViewModel = new EventBookingViewModel
{
EventTypes = new SelectList(methodToReturnAListToHere(), "ValueColumn", "DisplayColumn")
};
return View(eventBookingViewModel);
}
视图:
<div class="col-md-9">
@Html.DropDownListFor(model => model.EventType, Model.EventTypes, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.EventType)
</div>
在DropDownListFor中,第一个参数是您希望存储所选值的参数,第二个参数是您希望绑定的SelectList。
在MVC5中绑定下拉列表:
视图代码
@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>
控制器代码
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();
}
您想要填充事件列表吗?试试这些,也许对你有用
<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>