c# - MVC4 View 未绑定(bind)到 POST Controller 中的模型

标签 c# .net jquery asp.net-mvc-4 model-binding

应用 View :

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

应用程序 Controller :

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/用户:

@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>

当我单击保存按钮时,在应用程序 Controller 中,只有 FormCollection 有键(其中 21 个)。该模型不与数据绑定(bind)。

我在这里做错了什么?

最佳答案

试试这个,添加 {get;设置;}到你的模型

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

关于c# - MVC4 View 未绑定(bind)到 POST Controller 中的模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14168194/

相关文章:

c# - 如何刷新挂起的 FileSystemWatcher 事件?

javascript - 单击时隐藏/显示 div

javascript - 如何将输入加入到表单中

c# - 在Unity3D中读取NFC标签

c# - 同步两个 RichTextBox 的滚动位置?

C# SMO - 将表数据脚本化到文件。 throw 错误

javascript - 为什么我的 jqgrid 没有显示任何数据?

c# - 封装网络协议(protocol)

c# - 使用 SSL 的网络请求

.net - 随着应用程序运行时间的延长,.NET 单例的性能是否会降低?