MVC5中的DropDownList

时间:2014-07-18 14:40:36

标签: c# drop-down-menu asp.net-mvc-5

如何使用Code First从c#MVC5中的数据库填充下拉列表中的数据。

这就是我所尝试的:

型号:

 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>

我无法将“事件类型”视图更改为“下拉列表”。谁能请你帮忙。

3 个答案:

答案 0 :(得分:1)

我已经看到了使用数据填充下拉列表的几种不同方法,但我最终这样做的方式通常围绕将“事件类型”放入其自己的列表中,然后将其发送回视图中。模型。默认的scaffolded操作使用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。

答案 1 :(得分:1)

在MVC5中绑定DropDownList如下:

查看代码

@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();
}

答案 2 :(得分:0)

您想要填充活动列表吗?尝试这样的事可能它可能对你有用

          <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>