DropDownList模型属性在post上为空

时间:2014-12-01 13:49:02

标签: c# asp.net asp.net-mvc entity-framework asp.net-mvc-5

我正在尝试从模型属性的下拉列表中获取属性,但是在回发时值为null,尽管在下拉列表中选择了一个值。

这就是我所拥有的:

[HttpGet]
public ActionResult AddEditEvent(int? id)
{
    var eventViewModel = new EventViewModel();
    var projectEventModel = new projectEvent();
    if (id != null)
    {
        using (var db = new DBEntities())
        {
            projectEventModel = (from p in db.projectEvents
                              where p.eventID == id
                              select p).FirstOrDefault();
        }
    }

    eventViewModel.ProjectEvent = projectEventModel;

    using (var db = new DBEntities())
    {
        eventViewModel.eventTypes = (from p in db.eventTypes
                      select p).ToList();
    }

    return View(eventViewModel);
}

[HttpPost]
public ActionResult AddEditEvent(EventViewModel model)
{
    if (ModelState.IsValid)
    {
        using (var db = new DBEntities())
        {
        //save values
        }
    }
    return RedirectToAction("Events");
}

这是视图模型:

public class EventViewModel
    {
        public projectEvent ProjectEvent { get; set; }
        public IEnumerable<eventType> eventTypes { get; set; }
    }

使用db table中的EF创建的projectEvent类:

public partial class projectEvent
    {
        public projectEvent()
        {
            this.eventsToProjects = new HashSet<eventsToProject>();
        }

        public int eventID { get; set; }

        [Required]
        public Nullable<System.DateTime> eventDate { get; set; }

        [Required]
        public string eventName { get; set; }
        public Nullable<int> typeID { get; set; }

        [Required]
        public virtual eventType eventType { get; set; }
        public virtual ICollection<eventsToProject> eventsToProjects { get; set; }
    }

我正在使用的视图:

@model TTB.ViewModels.EventViewModel

@{
    Layout = "~/Views/Shared/_Layout_Main.cshtml";
}

<h2>Add/Edit Event</h2>

@using (Html.BeginForm("AddEditEvent", "Admin"))
{
    <div class="container">
        <div class="col-md-8">
            <div class="form-group">
                <label for="name">Name</label>
                @Html.TextBoxFor(m => m.ProjectEvent.eventName, new { @class = "form-control" })
                @Html.ValidationMessageFor(m => m.ProjectEvent.eventName)
            </div>
            <div class="form-group">
                <label for="name">Date</label>
                @Html.TextBoxFor(m => m.ProjectEvent.eventDate, new { @class = "form-control" })
                @Html.ValidationMessageFor(m => m.ProjectEvent.eventDate)
            </div>
            <div class="form-group">
                <label for="name">Event Type</label>
                @Html.DropDownListFor(v => v.ProjectEvent.eventType, new SelectList(Model.eventTypes, "eventTypeID", "eventTypeName"), new { @class = "form-control dropdown" })
            </div>
            @Html.HiddenFor(m => m.ProjectEvent.eventID)
        </div>
        <div class="col-md-8">
            <input type="submit" class="btn btn-success btn-lg btnSaveEdit" value="Save" />
        </div>
    </div>
}

在回发时,这是模型的外观:

enter image description here

知道我做错了什么吗?

0 个答案:

没有答案