viewmodel returns null on postback mvc 5

2019-01-20 03:22发布

问题:

I have this problem with a viewmodel, when the view is posted I'm getting null values for all the properties.

I've researched a bit, I can see multiple ppl have this problem, I tried some of the solutions but none of them works for me.

This is what I have:

Viewmodel:

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

Controller:

[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");
    }

View:

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

Any idea what am I doing wrong?

Thanks in advance, Laziale

UPDATE:

Rendered html output:

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

回答1:

It is because you have a property on your model called projectEvent and you refer to your model as projectEvent when it is passed back into the controller.

Change

public ActionResult AddEditEvent(EventViewModel projectEvent)

To

public ActionResult AddEditEvent(EventViewModel model)

(or another name of your choosing)

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