Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
229 views
in Technique[技术] by (71.8m points)

c# - viewmodel returns null on postback mvc 5

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

What am I doing wrong?

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...