3

アプリケーションビュー:

@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個)。モデルはデータにバインドされていません。

私はここで何が間違っているのですか?

4

2 に答える 2

2

これを試して、追加{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;}
}
于 2013-01-05T03:21:29.410 に答える
0

モデルのプロパティのアクセス修飾子を確認する必要がある場合もあります。私はこれと同じ状況に遭遇しました、そして私が私の財産にセッターを持っていた間、彼らは公の代わりに保護されました。私も正しい方向に向けてくれたメイトに感謝します。

于 2016-02-12T16:03:15.547 に答える