Ajax.BeginForm设置错误的操作

时间:2014-02-03 18:22:10

标签: asp.net-mvc razor

以下代码:

@using res = Resouces;
@model Model
@using (Ajax.BeginForm("Excuse", "Profile", new AjaxOptions() {
    HttpMethod = "POST",
    OnSuccess = "$('#excuse').modal('hide')"
}, new { @class = "form-horizontal" })) {
    <div class="modal fade" id="excuse" tabindex="-1" role="dialog" aria-hidden="true">
        <div class="modal-dialog modal-fullscreen form-horizontal">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h3 class="modal-title">@res.Profile.Excuse</h3>
                </div>
                <div class="modal-body">
                    @Html.HiddenFor(id => id.UserId)
                    <div class="form-group">
                        @Html.LabelFor(grp => grp.Reason, new { @class = "col-lg-1 control-label" })
                        <div class="col-lg-11">
                            @Html.TextAreaFor(grp => grp.Reason, new { @class = "form-control", style = "height: 400px;", rows = "10", placeholder = Html.DisplayNameFor(grp => grp.Reason) })
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button class="btn btn-default" data-dismiss="modal" type="button">@res.Resources.Cancel</button>
                    <button class="btn btn-primary" type="submit">@res.Profile.Excuse</button>
                </div>
            </div>
        </div>
    </div>
}

这是我得到的输出:

<form class="form-horizontal" id="form0" action="/gekkota/Presence/Excuse?Length=7" method="post" novalidate="novalidate" data-ajax-method="POST" data-ajax="true" data-ajax-success="$('#excuse').modal('hide')" data-ajax-url="/Profile/Excuse">    <div tabindex="-1" class="modal fade in" id="excuse" role="dialog" aria-hidden="false" style="display: block;">
        <div class="modal-dialog modal-fullscreen form-horizontal">
            <div class="modal-content">
                <div class="modal-header">
                    <button class="close" aria-hidden="true" type="button" data-dismiss="modal">×</button>
                    <h3 class="modal-title">Entschuldigen</h3>
                </div>
                <div class="modal-body">
                    <input name="UserId" id="UserId" type="hidden" value="1" data-val-required='Das Feld "UserId" ist erforderlich.' data-val="true" data-val-number='Das Feld "UserId" muss eine Zahl sein.'>
                    <div class="form-group">
                        <label class="col-lg-1 control-label" for="Reason">Begründung</label>
                        <div class="col-lg-11">
                            <textarea name="Reason" class="form-control" id="Reason" style="height: 400px;" placeholder="Begr&amp;#252;ndung" rows="10" cols="20">Begr&amp;#252;ndung</textarea>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button class="btn btn-default" type="button" data-dismiss="modal">Abbrechen</button>
                    <button class="btn btn-primary" type="submit">Entschuldigen</button>
                </div>
            </div>
        </div>
    </div>
</form>

这是我期望的输出:

<form class="form-horizontal" id="form0" action="/gekkota/Profile/Excuse?Length=7" method="post" novalidate="novalidate" data-ajax-method="POST" data-ajax="true" data-ajax-success="$('#excuse').modal('hide')" data-ajax-url="/Profile/Excuse">    <div tabindex="-1" class="modal fade in" id="excuse" role="dialog" aria-hidden="false" style="display: block;">
        <div class="modal-dialog modal-fullscreen form-horizontal">
            <div class="modal-content">
                <div class="modal-header">
                    <button class="close" aria-hidden="true" type="button" data-dismiss="modal">×</button>
                    <h3 class="modal-title">Entschuldigen</h3>
                </div>
                <div class="modal-body">
                    <input name="UserId" id="UserId" type="hidden" value="1" data-val-required='Das Feld "UserId" ist erforderlich.' data-val="true" data-val-number='Das Feld "UserId" muss eine Zahl sein.'>
                    <div class="form-group">
                        <label class="col-lg-1 control-label" for="Reason">Begründung</label>
                        <div class="col-lg-11">
                            <textarea name="Reason" class="form-control" id="Reason" style="height: 400px;" placeholder="Begr&amp;#252;ndung" rows="10" cols="20">Begr&amp;#252;ndung</textarea>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button class="btn btn-default" type="button" data-dismiss="modal">Abbrechen</button>
                    <button class="btn btn-primary" type="submit">Entschuldigen</button>
                </div>
            </div>
        </div>
    </div>
</form>

如何实现我期望的代码?

1 个答案:

答案 0 :(得分:6)

因为您没有调用正确的重载方法。尝试用5个参数调用一个。例如:

@using (Ajax.BeginForm("Excuse", "Profile",null, new AjaxOptions() {...}, new { @class = "form-horizontal" }))