nickornotto
nickornotto

Reputation: 2156

MVC model property passed as null

I have a strongly typed views where I pass editors for several properties eg.

public class BookingModel
{
    public FirstPropertyModel FirstProperty { get; set; }
    public SecondPropertyModel SecondProperty { get; set; }
    public ThirdPropertyModel ThirdProperty { get; set; }
}

@model MyWebsite.Models.BookingModel
@using (Html.BeginForm("Order", "Booking", FormMethod.Post, new { @id = "order_summary" }))
{
    @Html.EditorFor(model => model.FirstProperty, "_FirstProperty")
    @Html.EditorFor(model => model.SecondProperty, "_SecondProperty")
    @Html.EditorFor(model => model.ThirdProperty, "_ThirdProperty")
    <input type="submit" id="btnOrder" value="Order" />
}

All properties objects are passed to the action nicely but one property (First) which comes as null.

They all are within EditorTemplates and their views are also strongly types - use their own models.

ANy idea why is this happening?

Upvotes: 0

Views: 107

Answers (2)

nickornotto
nickornotto

Reputation: 2156

I had a problem with some model properties displaying on the property view, so after fixing the property null issue has been fixed. Thank you for replies

Upvotes: 0

JamieD77
JamieD77

Reputation: 13949

I'd try initializing/instantiating the BookingModel properties in the constructor to see if that helps

public class BookingModel
{
    public BookingModel()
    {
        FirstProperty = new FirstPropertyModel();
        SecondProperty = new SecondPropertyModel();
        ThirdProperty = new ThirdPropertyModel();
    }
    public FirstPropertyModel FirstProperty { get; set; }
    public SecondPropertyModel SecondProperty { get; set; }
    public ThirdPropertyModel ThirdProperty { get; set; }
}

Upvotes: 0

Related Questions