MVC4视图在POST时未绑定到控制器中的模型

本文关键字:控制器 模型 绑定 视图 POST MVC4 | 更新日期: 2023-09-27 17:50:48

应用程序视图:

@model Models.ApplicationModel
@using (Html.BeginForm())
{
    @Html.HiddenFor(m => m.SectionID);
    @Html.HiddenFor(m => m.CurrentSectionName);
    <div class="section" id="Terms">
        @Html.EditorFor(m => m.Term)
    </div>
    <div class="section" id="User">
        @Html.EditorFor(m => m.User)
    </div>
    <input type="submit" value="Save" />
}
@Scripts.Render("~/bundles/jqueryval")
<script type="text/javascript">
        $(function () {
            $('form').click(function () {
                if ($(this).valid()) {
                    $.ajax({
                        url: this.action,
                        type: this.method,
                        data: $(this).serialize(),
                        success: function (result) {
                            debugger;
                        }
                    });
                }
                return false;
            });
        });
</script>

应用程序模型:

public class ApplicationModel
{
    public int SectionID;
    public Term Term;
    public User User;
    public string CurrentSectionName;
}

应用程序控制器:

public ActionResult Save(ApplicationModel ApplicationModel, FormCollection fc) 
{
     return PartialView("Application", ApplicationModel);
}

/EditorTemplates/Term:

@model Data.Term
<fieldset>
    <legend>Term</legend>
    <div class="editor-label">
        @Html.LabelFor(model => model.Type)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Type)
        @Html.ValidationMessageFor(model => model.Type)
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.Length)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Length)
        @Html.ValidationMessageFor(model => model.Length)
    </div>
</fieldset>

/EditorTemplates/User:

@model Data.User
<fieldset>
    <legend>User</legend>
    <div class="editor-label">
        @Html.LabelFor(model => model.FirstName)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.FirstName)
        @Html.ValidationMessageFor(model => model.FirstName)
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.LastName)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.LastName)
        @Html.ValidationMessageFor(model => model.LastName)
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.MiddleInitial)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.MiddleInitial)
        @Html.ValidationMessageFor(model => model.MiddleInitial)
    </div>
</fieldset>

当我在应用程序控制器中单击保存按钮时,只有FormCollection有键(其中21个(。模型未与数据绑定。

我在这里做错了什么?

MVC4视图在POST时未绑定到控制器中的模型

试试这个,将{get;set;}添加到您的模型

public class ApplicationModel
{
    public int SectionID {get; set;}
    public Term Term {get; set;}
    public User User {get; set;}
    public string CurrentSectionName {get; set;}
}

您可能还需要检查Model属性上的访问修饰符。我也遇到了同样的情况,虽然我的房产上有设置者,但他们受到了保护,而不是公共场所。感谢Mate为我指明了正确的方向。