将自引用模型发布回控制器会导致null对象?

时间:2016-11-08 10:21:46

标签: c# asp.net-mvc asp.net-mvc-5

是否可以将永不停止的自引用模型从我的视图传递给控制器​​?当我提交表单时,Fiddler会捕获设置[index] .Settings [index] .Value等发布方法。但ActionResult Save(SettingSection设置)不捕获表单发布的值。以下是我的代码的详细信息。

这是我的自引用模型

public class SettingSection : Setting
{
    public List<SettingSection> Children { get; set; }
    public List<SettingSection> Settings { get; set; }

    #region Public Methods

    public string GetValue(string path)
    {
        // to do find value from json structure based on path
        return "";
    }

这是基类

public class Setting
{
    #region Fields

    private string _name;
    private string _text = "";
    private string _description = "";
    private string _enumerationName = "";
    private string _overrideValue = null;
    private DataType _settingType;
    private string _value;
    private PropertyInfo _propertyInfo;
    private Type _enumType;

    #endregion Fields

    #region Constructors

    public Setting()
    {
    }

    public Setting(string name, DataType settingType, string svalue)
    {
        _name = name;
        _settingType = settingType;
        _value = svalue;
    }

    #endregion Constructors

    #region Public Properties

    public string Description
    {
        get { return _description; }
        set { _description = value; }
    }

    public string EnumerationName
    {
        get { return _enumerationName; }
        set { _enumerationName = value; }
    }

    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }

    public string Text
    {
        get { return _text; }
        set { _text = value; }
    }

    public string Caption
    {
        get { return string.IsNullOrWhiteSpace(_text) ? _name : _text; }
    }

    public string OverrideValue
    {
        get { if (_overrideValue == null) return _value; return _overrideValue; }
        set { _overrideValue = value; }
    }

    public DataType SettingType
    {
        get { return _settingType; }
        set { _settingType = value; }
    }

    public string Value
    {
        get { return _value; }
        set { _value = value; }
    }

    #endregion Public Properties

    #region Public Methods

    #endregion Public Methods
}

这是我的观点

@using (Html.BeginForm("Save", "Settings", FormMethod.Post))
{
@Html.AntiForgeryToken()
{
    for (var a = 0; a < Model.Settings.Count; a++)
    {
        <div>@Model.Settings[a].Text</div>
        for (var e = 0; e < Model.Settings[a].Settings.Count; e++)
        {
            <div>@Model.Settings[a].Settings[e].Text</div>
            <div>
                @if (Model.Settings[a].Settings[e].Text == "true" || Model.Settings[a].Settings[e].Text == "false")
                {
                    @Html.CheckBoxFor(model => Convert.ToBoolean(Model.Settings[a].Settings[e].Value))
                }
                else
                {
                    @Html.TextBoxFor(model => Model.Settings[a].Settings[e].Value)
                }
            </div>
            <div>
                @for (var i = 0; i < Model.Settings[a].Settings[e].Children.Count; i++)
                {
                    <div style="margin-left: 50px">

                        @Model.Settings[a].Settings[e].Children[i].Text

                        @if (Model.Settings[a].Settings[e].Children[i].Value == "true" || Model.Settings[a].Settings[e].Children[i].Value == "false")
                        {
                            @Html.CheckBoxFor(model => Convert.ToBoolean(Model.Settings[a].Settings[e].Children[i].Value))
                        }
                        else
                        {
                            @Html.TextBoxFor(model => Model.Settings[a].Settings[e].Children[i].Value)
                        }
                    </div>
                }
            </div>
        }
    }
    <div class="row">
        <div class="col-md-2"></div>
        <div class="col-md-1">
            <button type="submit" class="form-control btn btn-success" value="Save">
                <i class="fa fa-save"></i>
                Save
            </button>

            <!--Html.ActionLink("Save", "Save", "Settings", null, new { settings = mod })-->
        </div>
        <div class="col-md-1">
            <a href="" class="form-control btn btn-danger"><i class="fa fa-times"></i><label>Cancel</label></a>
        </div>
    </div>
}

这是控制器

public ActionResult Save(SettingSection settings)
    {
        //settings == null, why?
        var b = settings;
        return View(settings);
    }

0 个答案:

没有答案