MVC模型属性传递为null

时间:2015-07-13 16:16:42

标签: asp.net-mvc model editorfor strongly-typed-view editortemplates

我有一个强类型的视图,我为几个属性传递编辑器,例如。

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

所有属性对象都很好地传递给动作,但是一个属性(First)变为null。

它们都在EditorTemplates中,它们的视图也是强类型 - 使用自己的模型。

我知道为什么会发生这种情况?

1 个答案:

答案 0 :(得分:0)

我尝试在构造函数中初始化/实例化BookingModel属性,看看是否有帮助

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