视图模型在回发 MVC 5 上返回 null

本文关键字:返回 null MVC 模型 视图 | 更新日期: 2023-09-27 18:32:19

我在视图模型上遇到了这个问题,当视图发布时,我得到了所有属性的空值。

我已经研究了一下,我可以看到多个人都有这个问题,我尝试了一些解决方案,但没有一个对我有用。

这是我所拥有的:

视图模型:

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

控制器:

[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 projectEvent)
    {
        if (ModelState.IsValid)
        {
            using (var db = new DBEntities())
            {
                db.projectEvents.AddOrUpdate(projectEvent);
                db.SaveChanges();
            }
        }
        return RedirectToAction("Events");
    }

视图:

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

我做错了什么?

更新

呈现的 html 输出:

<form action="/Admin/AddEditEvent" method="post">    <div class="container">
        <div class="col-md-8">
            <div class="form-group">
                <label for="name">Name</label>
                <input class="form-control" data-val="true" data-val-required="The eventName field is required." id="ProjectEvent_eventName" name="ProjectEvent.eventName" type="text" value="" />
                <span class="field-validation-valid" data-valmsg-for="ProjectEvent.eventName" data-valmsg-replace="true"></span>
            </div>
            <div class="form-group">
                <label for="name">Date</label>
                <input class="form-control" data-val="true" data-val-date="The field eventDate must be a date." data-val-required="The eventDate field is required." id="ProjectEvent_eventDate" name="ProjectEvent.eventDate" type="text" value="" />
                <span class="field-validation-valid" data-valmsg-for="ProjectEvent.eventDate" data-valmsg-replace="true"></span>
            </div>
            <div class="form-group">
                <label for="name">Event Type</label>
                <select class="form-control dropdown" id="ProjectEvent_eventType" name="ProjectEvent.eventType"><option value="1">movie</option>
</select>
            </div>
            <input data-val="true" data-val-number="The field eventID must be a number." data-val-required="The eventID field is required." id="ProjectEvent_eventID" name="ProjectEvent.eventID" type="hidden" value="0" />
        </div>
        <div class="col-md-8">
            <input type="submit" class="btn btn-success btn-lg btnSaveEdit" value="Save" />
        </div>
    </div>
</form>

视图模型在回发 MVC 5 上返回 null

这是因为您的模型上有一个名为 projectEvent 的属性,并且在将模型传递回控制器时将模型称为 projectEvent

改变

public ActionResult AddEditEvent(EventViewModel projectEvent)

public ActionResult AddEditEvent(EventViewModel model)

(或您选择的其他名称)

http://tech.pro/blog/1930/don-t-name-a-model-the-same-as-a-property-you-re-trying-bind-in-aspnet-mvc