Model is always NULL on form post to controller

Whenever I submit the form the model passed into the controller is NULL. I’ve spent ages looking at this. I think I am missing something fundamental here.

@model VisitorPortal.Models.ReinviteVisitorModel
@using (Html.BeginForm("CreateMeeting", "Home", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
    @Html.AntiForgeryToken()
    <h3>Reinvitation Details</h3>
    <div>The information entered below will be sent to the visitors email address @Model.Info.Email</div>
    <hr />
    @Html.ValidationSummary("", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(m => m.NewMeeting.Title, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.TextBoxFor(m => m.NewMeeting.Title, new { @class = "form-control" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.NewMeeting.StartTime, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.TextBoxFor(m => m.NewMeeting.StartTime, new { @class = "datetimepicker form-control" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.NewMeeting.EndTime, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.TextBoxFor(m => m.NewMeeting.EndTime, new { @class = "datetimepicker form-control" })
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            @Html.HiddenFor(m => m.NewMeeting.SubjectId, new { @Value = Model.Info.SubjectId })
            <input type="submit" class="btn btn-default" value="Send Invite" />
        </div>
    </div>
}

The Model is:

public class Meeting
{
    [Key]
    public int Id { get; set; }

    public string SubjectId { get; set; }

    [Required]
    [Display(Name = "Reason for invitation")]
    public string Title { get; set; }

    [Required]
    [Display(Name = "Start Time")]
    [DataType(DataType.Time)]
    public DateTime StartTime { get; set; }

    [Required]
    [Display(Name = "End Time")]
    [DataType(DataType.Time)]
    public DateTime EndTime { get; set; }

    public string HostEmail { get; set; }

    public string HostMobile { get; set; }    
}

public class MeetingsDBContext: DbContext
{
    public DbSet<Meeting> Meetings { get; set; }
}

public class ReinviteVisitorModel
{
    public Visitor Info;
    public Meeting NewMeeting;
    public List<Meeting> Meetings;
}

The Controller action is:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult CreateMeeting(Meeting meeting)
    {
        return RedirectToAction("ReinviteVisitor2", "Home", new { visitorId = meeting.SubjectId });
    }

I have fields in the model such as Id which I am expecting the database to populate which I was going to write in the the action CreateMeeting(). Do all fields in the Model have to be used in the form?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The model in your view is typeof ReinviteVisitorModel which means the signature of the POST method must match since your posting ReinviteVisitorModel

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CreateMeeting(ReinviteVisitorModel model)

alternatively you can use the Prefix property of BindAttribute to strip the NewMeeting prefix from the names of the form controls your are posting.

public ActionResult CreateMeeting([Bind(Prefix="NewMeeting")]Meeting model)

Side note: Remove new { @Value = Model.Info.SubjectId } from the hidden input and instead set the value of NewMeeting.SubjectId in the GET method before you pass the model to the view.

Method 2

You need to pass ReinviteVisitorModel model in your Action

Update you action with this:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CreateMeeting(ReinviteVisitorModel model)
{
    return RedirectToAction("ReinviteVisitor2", "Home", new { visitorId = model.NewMeeting.SubjectId });
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x